def ParseAndEval(code_str): arena = test_lib.MakeArena('<arith_parse_test.py>') parse_ctx = test_lib.InitParseContext(arena=arena) w_parser = test_lib.InitWordParser(code_str, arena=arena) # This is weird but works w_parser._Next(lex_mode_e.Arith) # Calling private method anode = w_parser.a_parser.Parse() print('node:', anode) mem = state.Mem('', [], arena, []) parse_opts, exec_opts, mutable_opts = state.MakeOpts(mem, None) mem.exec_opts = exec_opts state.InitMem(mem, {}, '0.1') splitter = split.SplitContext(mem) errfmt = ui.ErrorFormatter(arena) word_ev = word_eval.CompletionWordEvaluator(mem, exec_opts, mutable_opts, splitter, errfmt) arith_ev = sh_expr_eval.ArithEvaluator(mem, exec_opts, parse_ctx, arena) arith_ev.word_ev = word_ev return arith_ev.EvalToInt(anode)
def testStaticEvalWord(self): expr = r'\EOF' # Quoted here doc delimiter w_parser = test_lib.InitWordParser(expr) w = w_parser.ReadWord(lex_mode_e.ShCommand) ok, s, quoted = word_.StaticEval(w) self.assertEqual(True, ok) self.assertEqual('EOF', s) self.assertEqual(True, quoted)
def _MakeParser(code_str): # NOTE: We need the extra ]] token arena = test_lib.MakeArena('<bool_parse_test.py>') w_parser = test_lib.InitWordParser(code_str + ' ]]', arena=arena) w_parser._Next(lex_mode_e.DBracket) # for tests only p = bool_parse.BoolParser(w_parser) p._Next() return p
def _assertReadWordFailure(test, word_str, oil_at=False): print('\n---', word_str) w_parser = test_lib.InitWordParser(word_str, oil_at=oil_at) try: w = w_parser.ReadWord(lex_mode_e.ShCommand) except error.Parse as e: print('Got expected ParseError: %s' % e) else: w.PrettyPrint() test.fail('Expected a parser error, got %r' % w)
def _assertSpanForWord(test, word_str): arena = test_lib.MakeArena('word_parse_test.py') w_parser = test_lib.InitWordParser(word_str, arena=arena) w = _assertReadWordWithArena(test, w_parser) span_id = word_.LeftMostSpanForWord(w) print(word_str) print(span_id) if span_id != runtime.NO_SPID: span = arena.GetLineSpan(span_id) print(span)
def testUnicode(self): words = 'z \xce\xbb \xe4\xb8\x89 \xf0\x9f\x98\x98' w_parser = test_lib.InitWordParser(words) w = w_parser.ReadWord(lex_mode_e.ShCommand) self.assertEqual('z', w.parts[0].val) w = w_parser.ReadWord(lex_mode_e.ShCommand) self.assertEqual('\xce\xbb', w.parts[0].val) w = w_parser.ReadWord(lex_mode_e.ShCommand) self.assertEqual('\xe4\xb8\x89', w.parts[0].val) w = w_parser.ReadWord(lex_mode_e.ShCommand) self.assertEqual('\xf0\x9f\x98\x98', w.parts[0].val)
def ParseAndEval(code_str): arena = test_lib.MakeArena('<arith_parse_test.py>') w_parser = test_lib.InitWordParser(code_str, arena=arena) w_parser._Next(lex_mode_e.Arith) # Calling private method anode = w_parser._ReadArithExpr() # need the right lex state? print('node:', anode) mem = state.Mem('', [], {}, arena) exec_opts = state.ExecOpts(mem, None) splitter = split.SplitContext(mem) ev = word_eval.CompletionWordEvaluator(mem, exec_opts, splitter, arena) arith_ev = expr_eval.ArithEvaluator(mem, exec_opts, ev, arena) value = arith_ev.Eval(anode) return value
def testReadRegex(self): # Test that we get Id.Op_Newline code = '(foo|bar)' w_parser = test_lib.InitWordParser(code) w_parser.next_lex_mode = lex_mode_e.BashRegex # needed at beginning w = w_parser.ReadWord(lex_mode_e.BashRegex) assert w self.assertEqual('(', w.parts[0].val) self.assertEqual('foo', w.parts[1].val) self.assertEqual('|', w.parts[2].val) self.assertEqual('bar', w.parts[3].val) self.assertEqual(')', w.parts[4].val) self.assertEqual(5, len(w.parts)) w = w_parser.ReadWord(lex_mode_e.ShCommand) assert w self.assertEqual(Id.Eof_Real, w.id)
def testReadArith(self): CASES = [ '1 + 2', 'a + b', '$a * $b', '${a} * ${b}', '$(echo 1) * $(echo 2)', '`echo 1` + 2', '$((1 + 2)) * $((3 + 4))', "'single quoted'", # Allowed by oil but not bash '"${a}" + "${b}"', # Ditto '$# + $$', # This doesn't work but does in bash -- should be 15 #'$(( $(echo 1)$(echo 2) + 3 ))', '$(( x[0] < 5 ))', '$(( ++i ))', '$(( i++ ))', '$(( x -= 1))', '$(( x |= 1))', '$(( x[0] = 1 ))', '$(( 1 | 0 ))', '$((0x$size))', ] for expr in CASES: print('---') print(expr) print() w_parser = test_lib.InitWordParser(expr) w_parser._Next(lex_mode_e.Arith) # Can we remove this requirement? while True: w = w_parser.ReadWord(lex_mode_e.Arith) assert w is not None w.PrettyPrint() if word_.CommandId(w) in (Id.Eof_Real, Id.Unknown_Tok): break
def testRead(self): CASES = [ 'ls "foo"', '$(( 1 + 2 ))', '$(echo $(( 1 )) )', # OLD BUG: arith sub within command sub 'echo ${#array[@]} b', # Had a bug here 'echo $(( ${#array[@]} ))', # Bug here # Had a bug: unary minus #'${mounted_disk_regex:0:-1}', 'echo ${@%suffix}', # had a bug here '${@}', 'echo ${var,,}', 'echo ${var,,?}', # Line continuation tests '${\\\nfoo}', # VSub_1 '${foo\\\n}', # VSub_2 '${foo#\\\nyo}', # VS_ARG_UNQ '"${foo#\\\nyo}"', # VS_ARG_DQ ] for expr in CASES: print('---') print(expr) print() w_parser = test_lib.InitWordParser(expr) while True: w = w_parser.ReadWord(lex_mode_e.ShCommand) assert w is not None w.PrettyPrint() if word_.CommandId(w) == Id.Eof_Real: break
def ParseAndEval(code_str): arena = test_lib.MakeArena('<arith_parse_test.py>') w_parser = test_lib.InitWordParser(code_str, arena=arena) w_parser._Next(lex_mode_e.Arith) # Calling private method anode = w_parser._ReadArithExpr() # need the right lex state? print('node:', anode) mem = state.Mem('', [], arena, []) state.InitMem(mem, {}) parse_opts, exec_opts, mutable_opts = state.MakeOpts(mem, None) splitter = split.SplitContext(mem) errfmt = ui.ErrorFormatter(arena) word_ev = word_eval.CompletionWordEvaluator(mem, exec_opts, splitter, errfmt) arith_ev = sh_expr_eval.ArithEvaluator(mem, exec_opts, arena) arith_ev.word_ev = word_ev return arith_ev.EvalToInt(anode)
def testReadComment(self): # Test that we get Id.Op_Newline code = 'foo # comment\nbar #comment\n' w_parser = test_lib.InitWordParser(code) w = w_parser.ReadWord(lex_mode_e.ShCommand) assert w self.assertEqual('foo', w.parts[0].val) w = w_parser.ReadWord(lex_mode_e.ShCommand) assert w self.assertEqual(Id.Op_Newline, w.id) w = w_parser.ReadWord(lex_mode_e.ShCommand) assert w self.assertEqual('bar', w.parts[0].val) w = w_parser.ReadWord(lex_mode_e.ShCommand) assert w self.assertEqual(Id.Op_Newline, w.id) w = w_parser.ReadWord(lex_mode_e.ShCommand) assert w self.assertEqual(Id.Eof_Real, w.id)
def testHereDoc(self): w_parser = test_lib.InitWordParser("""\ ls foo # Multiple newlines and comments should be ignored ls bar """) print('--MULTI') w = w_parser.ReadWord(lex_mode_e.ShCommand) parts = [Tok(Id.Lit_Chars, 'ls')] test_lib.AssertAsdlEqual(self, compound_word(parts), w) w = w_parser.ReadWord(lex_mode_e.ShCommand) parts = [Tok(Id.Lit_Chars, 'foo')] test_lib.AssertAsdlEqual(self, compound_word(parts), w) w = w_parser.ReadWord(lex_mode_e.ShCommand) t = Tok(Id.Op_Newline, None) test_lib.AssertAsdlEqual(self, t, w) w = w_parser.ReadWord(lex_mode_e.ShCommand) parts = [Tok(Id.Lit_Chars, 'ls')] test_lib.AssertAsdlEqual(self, compound_word(parts), w) w = w_parser.ReadWord(lex_mode_e.ShCommand) parts = [Tok(Id.Lit_Chars, 'bar')] test_lib.AssertAsdlEqual(self, compound_word(parts), w) w = w_parser.ReadWord(lex_mode_e.ShCommand) t = Tok(Id.Op_Newline, None) test_lib.AssertAsdlEqual(self, t, w) w = w_parser.ReadWord(lex_mode_e.ShCommand) t = Tok(Id.Eof_Real, '') test_lib.AssertAsdlEqual(self, t, w)
def testMultiLine(self): w_parser = test_lib.InitWordParser("""\ ls foo # Multiple newlines and comments should be ignored ls bar """) print('--MULTI') w = w_parser.ReadWord(lex_mode_e.ShCommand) parts = [word_part.Literal(token(Id.Lit_Chars, 'ls'))] test_lib.AssertAsdlEqual(self, word.Compound(parts), w) w = w_parser.ReadWord(lex_mode_e.ShCommand) parts = [word_part.Literal(token(Id.Lit_Chars, 'foo'))] test_lib.AssertAsdlEqual(self, word.Compound(parts), w) w = w_parser.ReadWord(lex_mode_e.ShCommand) t = token(Id.Op_Newline, '\n') test_lib.AssertAsdlEqual(self, word.Token(t), w) w = w_parser.ReadWord(lex_mode_e.ShCommand) parts = [word_part.Literal(token(Id.Lit_Chars, 'ls'))] test_lib.AssertAsdlEqual(self, word.Compound(parts), w) w = w_parser.ReadWord(lex_mode_e.ShCommand) parts = [word_part.Literal(token(Id.Lit_Chars, 'bar'))] test_lib.AssertAsdlEqual(self, word.Compound(parts), w) w = w_parser.ReadWord(lex_mode_e.ShCommand) t = token(Id.Op_Newline, '\n') test_lib.AssertAsdlEqual(self, word.Token(t), w) w = w_parser.ReadWord(lex_mode_e.ShCommand) t = token(Id.Eof_Real, '') test_lib.AssertAsdlEqual(self, word.Token(t), w)
def _assertReadWord(test, word_str, oil_at=False): print('\n---', word_str) arena = test_lib.MakeArena('word_parse_test.py') w_parser = test_lib.InitWordParser(word_str, arena=arena, oil_at=oil_at) w = _assertReadWordWithArena(test, w_parser) return w