|
| 1 | +# -*- coding: utf-8 -*- |
| 2 | + |
| 3 | +# Copyright (c) 2010-2016, MIT Probabilistic Computing Project |
| 4 | +# |
| 5 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | +# you may not use this file except in compliance with the License. |
| 7 | +# You may obtain a copy of the License at |
| 8 | +# |
| 9 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +# |
| 11 | +# Unless required by applicable law or agreed to in writing, software |
| 12 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | +# See the License for the specific language governing permissions and |
| 15 | +# limitations under the License. |
| 16 | + |
| 17 | +from collections import namedtuple |
| 18 | + |
| 19 | +from bayeslite.exception import BQLParseError |
| 20 | +from bayeslite.util import casefold |
| 21 | + |
| 22 | +import grammar |
| 23 | + |
| 24 | +''' |
| 25 | +grep -o 'K_[A-Z][A-Z0-9_]*' < grammar.y | sort -u | awk ' |
| 26 | +{ |
| 27 | + sub("^K_", "", $1); |
| 28 | + printf(" '\''%s'\'': grammar.K_%s,\n", tolower($1), $1); |
| 29 | +}' |
| 30 | +''' |
| 31 | + |
| 32 | +KEYWORDS = { |
| 33 | + 'skip': grammar.K_SKIP, |
| 34 | + 'variables': grammar.K_VARIABLES, |
| 35 | +} |
| 36 | + |
| 37 | +PUNCTUATION = { |
| 38 | + ',': grammar.T_COMMA, |
| 39 | + ';': grammar.T_SEMI, |
| 40 | +} |
| 41 | + |
| 42 | +def parse(tokens): |
| 43 | + semantics = CGpmAnalyzeSemantics() |
| 44 | + parser = grammar.Parser(semantics) |
| 45 | + for token in tokenize(tokens): |
| 46 | + semantics.context.append(token) |
| 47 | + if len(semantics.context) > 10: |
| 48 | + semantics.context.pop(0) |
| 49 | + parser.feed(token) |
| 50 | + if semantics.failed or semantics.errors: |
| 51 | + raise BQLParseError('\n'.join(semantics.errors)) |
| 52 | + assert semantics.phrases is not None |
| 53 | + return semantics.phrases |
| 54 | + |
| 55 | + |
| 56 | +def tokenize(tokens): |
| 57 | + for token in tokens: |
| 58 | + if isinstance(token, str): |
| 59 | + if casefold(token) in KEYWORDS: |
| 60 | + yield KEYWORDS[casefold(token)], token |
| 61 | + elif token in PUNCTUATION: |
| 62 | + yield PUNCTUATION[token], token |
| 63 | + else: # XXX check for alphanumeric/_ |
| 64 | + yield grammar.L_NAME, token |
| 65 | + elif isinstance(token, (int, float)): |
| 66 | + yield grammar.L_NUMBER, token |
| 67 | + else: |
| 68 | + raise IOError('Invalid token: %r' % (token,)) |
| 69 | + yield 0, '' # EOF |
| 70 | + |
| 71 | + |
| 72 | +class CGpmAnalyzeSemantics(object): |
| 73 | + def __init__(self): |
| 74 | + self.context = [] |
| 75 | + self.errors = [] |
| 76 | + self.failed = False |
| 77 | + self.phrases = None |
| 78 | + |
| 79 | + def accept(self): |
| 80 | + pass |
| 81 | + def parse_failed(self): |
| 82 | + self.failed = True |
| 83 | + |
| 84 | + def syntax_error(self, (token, text)): |
| 85 | + if token == -1: # error |
| 86 | + self.errors.append("Syntax error near [%s] after [%s]" % ( |
| 87 | + text, ' '.join([str(t) for (_t, t) in self.context[:-1]]))) |
| 88 | + |
| 89 | + def p_anlaysis_start(self, ps): self.phrases = ps |
| 90 | + |
| 91 | + def p_phrases_one(self, p): return [p] if p else [] |
| 92 | + def p_phrases_many(self, ps, p): |
| 93 | + if p: ps.append(p) |
| 94 | + return ps |
| 95 | + |
| 96 | + def p_phrase_none(self,): return None |
| 97 | + def p_phrase_variables(self, cols): return Variables(cols) |
| 98 | + def p_phrase_skip(self, cols): return Skip(cols) |
| 99 | + |
| 100 | + def p_column_list_one(self, col): return [col] |
| 101 | + def p_column_list_many(self, cols, col): cols.append(col); return cols |
| 102 | + def p_column_name_n(self, name): return name |
| 103 | + |
| 104 | +Variables = namedtuple('Variables', [ |
| 105 | + 'vars', |
| 106 | +]) |
| 107 | + |
| 108 | +Skip = namedtuple('Skip', [ |
| 109 | + 'vars', |
| 110 | +]) |
0 commit comments