parAstParser.c 2.6 KB
Newer Older
X
Xiaoyu Wang 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
/*
 * Copyright (c) 2019 TAOS Data, Inc. <jhtao@taosdata.com>
 *
 * This program is free software: you can use, redistribute, and/or modify
 * it under the terms of the GNU Affero General Public License, version 3
 * or later ("AGPL"), as published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 */

wafwerar's avatar
wafwerar 已提交
16
#include "os.h"
X
Xiaoyu Wang 已提交
17
#include "parInt.h"
X
Xiaoyu Wang 已提交
18

X
Xiaoyu Wang 已提交
19 20
#include "parAst.h"
#include "parToken.h"
X
Xiaoyu Wang 已提交
21 22 23 24

typedef void* (*FMalloc)(size_t);
typedef void (*FFree)(void*);

X
Xiaoyu Wang 已提交
25
extern void* ParseAlloc(FMalloc);
X
Xiaoyu Wang 已提交
26 27 28
extern void  Parse(void*, int, SToken, void*);
extern void  ParseFree(void*, FFree);
extern void  ParseTrace(FILE*, char*);
X
Xiaoyu Wang 已提交
29

X
Xiaoyu Wang 已提交
30
int32_t parse(SParseContext* pParseCxt, SQuery** pQuery) {
31 32
  SAstCreateContext cxt;
  initAstCreateContext(pParseCxt, &cxt);
X
Xiaoyu Wang 已提交
33
  void*   pParser = ParseAlloc((FMalloc)taosMemoryMalloc);
X
Xiaoyu Wang 已提交
34 35 36 37
  int32_t i = 0;
  while (1) {
    SToken t0 = {0};
    if (cxt.pQueryCxt->pSql[i] == 0) {
X
Xiaoyu Wang 已提交
38
      Parse(pParser, 0, t0, &cxt);
X
Xiaoyu Wang 已提交
39 40
      goto abort_parse;
    }
X
Xiaoyu Wang 已提交
41 42
    t0.n = tGetToken((char*)&cxt.pQueryCxt->pSql[i], &t0.type);
    t0.z = (char*)(cxt.pQueryCxt->pSql + i);
X
Xiaoyu Wang 已提交
43 44 45
    i += t0.n;

    switch (t0.type) {
46 47
      case TK_NK_SPACE:
      case TK_NK_COMMENT: {
X
Xiaoyu Wang 已提交
48 49
        break;
      }
50
      case TK_NK_SEMI: {
X
Xiaoyu Wang 已提交
51
        Parse(pParser, 0, t0, &cxt);
52 53
        goto abort_parse;
      }
54
      case TK_NK_ILLEGAL: {
X
Xiaoyu Wang 已提交
55
        snprintf(cxt.pQueryCxt->pMsg, cxt.pQueryCxt->msgLen, "unrecognized token: \"%s\"", t0.z);
56
        cxt.errCode = TSDB_CODE_PAR_SYNTAX_ERROR;
X
Xiaoyu Wang 已提交
57 58
        goto abort_parse;
      }
59 60 61
      case TK_NK_HEX:
      case TK_NK_OCT:
      case TK_NK_BIN: {
X
Xiaoyu Wang 已提交
62
        snprintf(cxt.pQueryCxt->pMsg, cxt.pQueryCxt->msgLen, "unsupported token: \"%s\"", t0.z);
63
        cxt.errCode = TSDB_CODE_PAR_SYNTAX_ERROR;
X
Xiaoyu Wang 已提交
64 65 66
        goto abort_parse;
      }
      default:
X
Xiaoyu Wang 已提交
67
        // ParseTrace(stdout, "");
X
Xiaoyu Wang 已提交
68
        Parse(pParser, t0.type, t0, &cxt);
69
        if (TSDB_CODE_SUCCESS != cxt.errCode) {
X
Xiaoyu Wang 已提交
70 71 72 73 74 75
          goto abort_parse;
        }
    }
  }

abort_parse:
wafwerar's avatar
wafwerar 已提交
76
  ParseFree(pParser, (FFree)taosMemoryFree);
77
  if (TSDB_CODE_SUCCESS == cxt.errCode) {
wafwerar's avatar
wafwerar 已提交
78
    *pQuery = taosMemoryCalloc(1, sizeof(SQuery));
79 80 81
    if (NULL == *pQuery) {
      return TSDB_CODE_OUT_OF_MEMORY;
    }
82
    (*pQuery)->pRoot = cxt.pRootNode;
D
dapan1121 已提交
83
    (*pQuery)->placeholderNum = cxt.placeholderNo;
X
Xiaoyu Wang 已提交
84
    TSWAP((*pQuery)->pPlaceholderValues, cxt.pPlaceholderValues);
X
Xiaoyu Wang 已提交
85
  }
X
Xiaoyu Wang 已提交
86
  taosArrayDestroy(cxt.pPlaceholderValues);
87
  return cxt.errCode;
X
Xiaoyu Wang 已提交
88
}