|
6 | 6 |
|
7 | 7 | import unittest
|
8 | 8 |
|
| 9 | +#from _devbuild.gen import grammar_nt # names for integer nonterminal IDs |
9 | 10 | from _devbuild.gen.id_kind_asdl import Kind
|
| 11 | +from _devbuild.gen.syntax_asdl import source |
| 12 | + |
10 | 13 | from core.meta import ID_SPEC
|
11 |
| -#from frontend import lex |
12 |
| -#from pgen2.pgen2_main import OilTokenDef # module under test |
| 14 | +from core import alloc |
| 15 | +from core import meta |
| 16 | +from core import pyutil |
| 17 | +from frontend import parse_lib |
| 18 | +from frontend import reader |
| 19 | + |
13 | 20 |
|
| 21 | +class ExprParseTest(unittest.TestCase): |
14 | 22 |
|
15 |
| -class FooTest(unittest.TestCase): |
16 | 23 | def setUp(self):
|
17 |
| - pass |
| 24 | + """Done on every test.""" |
| 25 | + self.arena = alloc.Arena() |
| 26 | + self.arena.PushSource(source.Unused('')) |
| 27 | + |
| 28 | + loader = pyutil.GetResourceLoader() |
| 29 | + oil_grammar = meta.LoadOilGrammar(loader) |
| 30 | + |
| 31 | + self.parse_ctx = parse_lib.ParseContext(self.arena, {}, oil_grammar, |
| 32 | + one_pass_parse=True) |
| 33 | + |
| 34 | + def _ParseOsh(self, code_str): |
| 35 | + """Parse a line of OSH, which can include Oil assignments.""" |
| 36 | + line_reader = reader.StringLineReader(code_str, self.arena) |
| 37 | + # the OSH parser hooks into the Oil parser |
| 38 | + c_parser = self.parse_ctx.MakeOshParser(line_reader) |
| 39 | + node = c_parser.ParseLogicalLine() |
| 40 | + node.PrettyPrint() |
| 41 | + return node |
| 42 | + |
| 43 | + def _ParseOilExpression(self, code_str): |
| 44 | + """Convenient shortcut.""" |
| 45 | + node = self._ParseOsh('var x = %s\n' % code_str) |
| 46 | + |
| 47 | + def testPythonLike(self): |
| 48 | + # This works. |
| 49 | + node = self._ParseOsh('var x = y + 2 * 3;') |
18 | 50 |
|
19 |
| - def tearDown(self): |
20 |
| - pass |
| 51 | + # The lexer isn't handling single quotes yet. |
| 52 | + #node = self._ParseOsh(r"var x = 'one\ntwo\n';") |
21 | 53 |
|
22 |
| - def testOilTokenDef(self): |
23 |
| - # Used for |
24 |
| - #tok_def = OilTokenDef() |
| 54 | + # NOTE: C-escapes aren't parsed properly. |
| 55 | + node = self._ParseOsh(r'var x = "one\ntwo\n";') |
25 | 56 |
|
26 |
| - # NOTE: These overlap with Kind.Op. |
| 57 | + # These raise NotImplementedError |
27 | 58 |
|
28 |
| - # We need ID_SPEC.ExprOperators(), which has Op, Arith, and Expr kinds. |
| 59 | + #node = self._ParseOsh('var x = [1,2,3];') |
| 60 | + #node = self._ParseOilExpression('[]') |
| 61 | + #node = self._ParseOilExpression('{foo: bar}') |
29 | 62 |
|
30 |
| - # We don't have: |
31 |
| - # LexerPairs(Kind.Op) |
32 |
| - # LexerPairs(Kind.Expr) |
| 63 | + def testOtherExpr(self): |
| 64 | + """Some examples copied from pgen2/pgen2-test.sh mode-test.""" |
33 | 65 |
|
34 |
| - # Because we really need a lookup for a MODE. |
35 |
| - # Problem: _UNQUOTED is used in both DBracket and ShCommand mode. |
| 66 | + node = self._ParseOsh('@[1 2 3];') |
36 | 67 |
|
| 68 | + CASES = [ |
| 69 | + '@[1 2 3]', |
| 70 | + '$/ x /', |
| 71 | + '$/ "." [a-z A-Z] y /', |
| 72 | + '$[echo hi]', |
| 73 | + '$(1 + 2)', |
| 74 | + '${x}', |
| 75 | + '"quoted ${x}"', |
| 76 | + ] |
37 | 77 |
|
38 |
| - arith = ID_SPEC.LexerPairs(Kind.Arith) |
39 |
| - print(arith) |
| 78 | + # array literal |
| 79 | + for c in CASES: |
| 80 | + node = self._ParseOilExpression(c) |
40 | 81 |
|
41 |
| - # Doesn't have one. |
42 |
| - #left = ID_SPEC.LexerPairs(Kind.Left) |
43 |
| - #print(left) |
| 82 | + def testLexer(self): |
| 83 | + # NOTE: Kind.Expr for Oil doesn't have LexerPairs |
| 84 | + pairs = ID_SPEC.LexerPairs(Kind.Arith) |
| 85 | + for p in pairs: |
| 86 | + #print(p) |
| 87 | + pass |
44 | 88 |
|
45 | 89 |
|
46 | 90 | if __name__ == '__main__':
|
|
0 commit comments