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/>.
 */

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 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;
}