astParse.c 2.9 KB
Newer Older
X
Xiaoyu Wang 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
/*
 * 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/>.
 */

#include "parserInt.h"

#include "astCreateFuncs.h"
#include "ttoken.h"

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

extern void* NewParseAlloc(FMalloc);
extern void NewParse(void*, int, SToken, void*);
extern void NewParseFree(void*, FFree);
extern void NewParseTrace(FILE*, char*);

29 30 31 32 33 34 35 36 37 38 39 40
static void setQuery(SAstCreateContext* pCxt, SQuery* pQuery) {
  pQuery->pRoot = pCxt->pRootNode;
  ENodeType type = nodeType(pCxt->pRootNode);
  if (QUERY_NODE_SELECT_STMT == type) {
    pQuery->haveResultSet = true;
    pQuery->directRpc = false;
  } else if (QUERY_NODE_CREATE_TABLE_STMT == type) {
    pQuery->haveResultSet = false;
    pQuery->directRpc = false;
  } else {
    pQuery->haveResultSet = false;
    pQuery->directRpc = true;
X
Xiaoyu Wang 已提交
41
  }
42
  pQuery->msgType = (QUERY_NODE_CREATE_TABLE_STMT == type ? TDMT_VND_CREATE_TABLE : TDMT_VND_QUERY);
X
Xiaoyu Wang 已提交
43 44 45
}

int32_t doParse(SParseContext* pParseCxt, SQuery** pQuery) {
46 47
  SAstCreateContext cxt;
  initAstCreateContext(pParseCxt, &cxt);
X
Xiaoyu Wang 已提交
48 49 50 51 52 53 54 55
  void *pParser = NewParseAlloc(malloc);
  int32_t i = 0;
  while (1) {
    SToken t0 = {0};
    if (cxt.pQueryCxt->pSql[i] == 0) {
      NewParse(pParser, 0, t0, &cxt);
      goto abort_parse;
    }
56
    t0.n = tGetToken((char *)&cxt.pQueryCxt->pSql[i], &t0.type);
X
Xiaoyu Wang 已提交
57 58 59 60 61 62 63 64
    t0.z = (char *)(cxt.pQueryCxt->pSql + i);
    i += t0.n;

    switch (t0.type) {
      case TK_SPACE:
      case TK_COMMENT: {
        break;
      }
65 66 67 68
      case TK_SEMI: {
        NewParse(pParser, 0, t0, &cxt);
        goto abort_parse;
      }
X
Xiaoyu Wang 已提交
69 70 71 72 73 74 75 76 77 78 79 80 81 82 83
      case TK_QUESTION:
      case TK_ILLEGAL: {
        snprintf(cxt.pQueryCxt->pMsg, cxt.pQueryCxt->msgLen, "unrecognized token: \"%s\"", t0.z);
        cxt.valid = false;
        goto abort_parse;
      }
      case TK_HEX:
      case TK_OCT:
      case TK_BIN: {
        snprintf(cxt.pQueryCxt->pMsg, cxt.pQueryCxt->msgLen, "unsupported token: \"%s\"", t0.z);
        cxt.valid = false;
        goto abort_parse;
      }
      default:
        NewParse(pParser, t0.type, t0, &cxt);
X
Xiaoyu Wang 已提交
84
        // NewParseTrace(stdout, "");
X
Xiaoyu Wang 已提交
85 86 87 88 89 90 91 92 93 94
        if (!cxt.valid) {
          goto abort_parse;
        }
    }
  }

abort_parse:
  NewParseFree(pParser, free);
  if (cxt.valid) {
    *pQuery = calloc(1, sizeof(SQuery));
95 96 97
    if (NULL == *pQuery) {
      return TSDB_CODE_OUT_OF_MEMORY;
    }
98
    setQuery(&cxt, *pQuery);
X
Xiaoyu Wang 已提交
99 100 101
  }
  return cxt.valid ? TSDB_CODE_SUCCESS : TSDB_CODE_FAILED;
}