parAstParser.c 2.4 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/>.
 */

X
Xiaoyu Wang 已提交
16
#include "parInt.h"
X
Xiaoyu Wang 已提交
17

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

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

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

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

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

abort_parse:
wafwerar's avatar
wafwerar 已提交
76
  ParseFree(pParser, (FFree)taosMemoryFree);
X
Xiaoyu Wang 已提交
77
  if (cxt.valid) {
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;
X
Xiaoyu Wang 已提交
83 84 85
  }
  return cxt.valid ? TSDB_CODE_SUCCESS : TSDB_CODE_FAILED;
}