1 | # Data types for the Oils AST, aka "Lossless Syntax Tree".
|
2 | #
|
3 | # Invariant: the source text can be reconstructed byte-for-byte from this tree.
|
4 | # The test/lossless.sh suite verifies this.
|
5 |
|
6 | # We usually try to preserve the physical order of the source in the ASDL
|
7 | # fields. One exception is the order of redirects:
|
8 | #
|
9 | # echo >out.txt hi
|
10 | # # versus
|
11 | # echo hi >out.txt
|
12 |
|
13 | # Unrepresented:
|
14 | # - let arithmetic (rarely used)
|
15 | # - coprocesses # one with arg and one without
|
16 | # - select block
|
17 |
|
18 | # Possible refactorings:
|
19 | #
|
20 | # # %CompoundWord as first class variant:
|
21 | # bool_expr = WordTest %CompoundWord | ...
|
22 | #
|
23 | # # Can DoubleQuoted have a subset of parts compared with CompoundWord?
|
24 | # string_part = ... # subset of word_part
|
25 | #
|
26 | # - Distinguish word_t with BracedTree vs. those without? seq_word_t?
|
27 |
|
28 | module syntax
|
29 | {
|
30 | use core value {
|
31 | value LiteralBlock
|
32 | }
|
33 |
|
34 | # More efficient than the List[bool] pattern we've been using
|
35 | BoolParamBox = (bool b)
|
36 | IntParamBox = (int i)
|
37 |
|
38 | # core/main_loop.py
|
39 | parse_result = EmptyLine | Eof | Node(command cmd)
|
40 |
|
41 | # 'source' represents the location of a line / token.
|
42 | source =
|
43 | Interactive
|
44 | | Headless
|
45 | | Unused(str comment) # completion and history never show parse errors?
|
46 | | CFlag
|
47 | | Stdin(str comment)
|
48 |
|
49 | # MainFile is for main.{osh,ysh}, as well as oshrc/yshrc. They're files
|
50 | # loaded directly by the shell.
|
51 | | MainFile(str path)
|
52 | # A file loaded by 'source' or 'use'.
|
53 | # TODO: provide a chain of locations back to the sourced script! I guess
|
54 | # the debug_stack can do that too.
|
55 | | OtherFile(str path, loc location)
|
56 |
|
57 | # Code parsed from a word. (TODO: rename source.Word?)
|
58 | # used for 'eval arg', 'trap arg', 'printf arg',
|
59 | # parseCommand() - this is a string?
|
60 | # dynamic LHS - move this to Reparsed?
|
61 | # complete -W
|
62 | | Dynamic(str what, loc location)
|
63 |
|
64 | # Point to the original variable reference
|
65 | | VarRef(Token orig_tok)
|
66 |
|
67 | # code parsed from the value of a variable
|
68 | # used for $PS1 $PROMPT_COMMAND
|
69 | | Variable(str var_name, loc location)
|
70 |
|
71 | # alias expansion (location of first word)
|
72 | | Alias(str argv0, loc argv0_loc)
|
73 |
|
74 | # 2 kinds of reparsing: backticks, and x+1 in a[x+1]=y
|
75 | | Reparsed(str what, Token left_token, Token right_token)
|
76 |
|
77 | # For --location-str
|
78 | | Synthetic(str s)
|
79 |
|
80 | SourceLine = (int line_num, str content, source src)
|
81 |
|
82 | # Note that ASDL generates:
|
83 | # typedef uint16_t Id_t;
|
84 | # So Token is
|
85 | # 8 bytes GC header + 2 + 2 + 4 + 8 + 8 = 32 bytes on 64-bit machines
|
86 | #
|
87 | # We transpose (id, col, length) -> (id, length, col) for C struct packing.
|
88 | Token = (id id, uint16 length, int col, SourceLine? line, str? tval)
|
89 |
|
90 | # I wanted to get rid of Token.tval with this separate WideToken type, but it
|
91 | # is more efficient if word_part.Literal %Token literally is the same thing
|
92 | # that comes out of the lexer. Otherwise we have extra garbage.
|
93 |
|
94 | # WideToken = (id id, int length, int col, SourceLine? line, str? tval)
|
95 |
|
96 | # Slight ASDL bug: CompoundWord has to be defined before using it as a shared
|
97 | # variant. The _product_counter algorithm should be moved into a separate
|
98 | # tag-assigning pass, and shared between gen_python.py and gen_cpp.py.
|
99 | CompoundWord = (List[word_part] parts)
|
100 |
|
101 | # Source location for errors
|
102 | loc =
|
103 | Missing # equivalent of runtime.NO_SPID
|
104 | | Token %Token
|
105 | # Very common case: argv arrays need original location
|
106 | | ArgWord %CompoundWord
|
107 | | WordPart(word_part p)
|
108 | | Word(word w)
|
109 | | Arith(arith_expr a)
|
110 | # e.g. for errexit blaming
|
111 | | Command(command c)
|
112 | # the location of a token that's too long
|
113 | | TokenTooLong(SourceLine line, id id, int length, int col)
|
114 |
|
115 | # debug_frame_t is an EXECUTION stack (proc func source use eval), while
|
116 | # source_t (in some cases) is like a PARSING stack (files, strings from vars,
|
117 | # etc.)
|
118 | debug_frame =
|
119 | # OSH: main_filename => BASH_SOURCE
|
120 | MainFile(str main_filename)
|
121 | # YSH
|
122 | | Dummy # -c or stdin, not used by BASH_* vars
|
123 | # Note: we could have more "frame 0" than MainFile and Dummy -
|
124 | # - Completion hooks - dev.Tracer is missing these
|
125 | # - PS1
|
126 | # - PROMPT_COMMAND
|
127 |
|
128 | # OSH: call_tok => BASH_LINENO, source_name => BASH_SOURCE
|
129 | | Source(CompoundWord source_loc, str source_name)
|
130 |
|
131 | # OSH: call_tok => BASH_LINENO, def_tok => BASH_SOURCE
|
132 | # YSH: procs
|
133 | | ProcLike(CompoundWord invoke_loc, Token def_tok, str proc_name)
|
134 |
|
135 | # for io->eval, myfunc()
|
136 | | Token %Token
|
137 |
|
138 | # For 'eval', 'use', ...
|
139 | | CompoundWord %CompoundWord
|
140 |
|
141 | # Special frame added when running 'trap ERR', for more info, and as a sentinel
|
142 | | BeforeErrTrap(Token tok)
|
143 |
|
144 | #
|
145 | # Shell language
|
146 | #
|
147 |
|
148 | bracket_op =
|
149 | WholeArray(id op_id) # * or @
|
150 | | ArrayIndex(arith_expr expr)
|
151 |
|
152 | suffix_op =
|
153 | Nullary %Token # ${x@Q} or ${!prefix@} (which also has prefix_op)
|
154 | | Unary(Token op, rhs_word arg_word) # e.g. ${v:-default}
|
155 | # TODO: Implement YSH ${x|html} and ${x %.3f}
|
156 | | Static(Token tok, str arg)
|
157 | | PatSub(CompoundWord pat, rhs_word replace, id replace_mode, Token slash_tok)
|
158 | # optional begin is arith_expr.EmptyZero
|
159 | # optional length is None, because it's handled in a special way
|
160 | | Slice(arith_expr begin, arith_expr? length)
|
161 |
|
162 | BracedVarSub = (
|
163 | Token left, # in dynamic ParseVarRef, same as name_tok
|
164 | Token name_tok, # location for the name
|
165 | str var_name, # the name - TODO: remove this, use LazyStr() instead
|
166 | Token? prefix_op, # prefix # or ! operators
|
167 | bracket_op? bracket_op,
|
168 | suffix_op? suffix_op,
|
169 | Token right # in dynamic ParseVarRef, same as name_tok
|
170 | )
|
171 |
|
172 | # Variants:
|
173 | # - Look at left token ID for $'' c'' vs r'' '' e.g. Id.Left_DollarSingleQuote
|
174 | # - And """ and ''' e.g. Id.Left_TDoubleQuote
|
175 | DoubleQuoted = (Token left, List[word_part] parts, Token right)
|
176 |
|
177 | # Consider making str? sval LAZY, like lexer.LazyStr(tok)
|
178 | SingleQuoted = (Token left, str sval, Token right)
|
179 |
|
180 | # e.g. Id.VSub_QMark, Id.VSub_DollarName $foo with lexer.LazyStr()
|
181 | SimpleVarSub = (Token tok)
|
182 |
|
183 | CommandSub = (Token left_token, command child, Token right)
|
184 |
|
185 | # - can contain word.BracedTree
|
186 | # - no 'Token right' for now, doesn't appear to be used
|
187 | ShArrayLiteral = (Token left, List[word] words, Token right)
|
188 |
|
189 | # Unevaluated, typed arguments for func and proc.
|
190 | # Note that ...arg is expr.Spread.
|
191 | ArgList = (
|
192 | Token left, List[expr] pos_args,
|
193 | Token? semi_tok, List[NamedArg] named_args,
|
194 | Token? semi_tok2, expr? block_expr,
|
195 | Token right
|
196 | )
|
197 |
|
198 | AssocPair = (CompoundWord key, CompoundWord value)
|
199 |
|
200 | word_part =
|
201 | ShArrayLiteral %ShArrayLiteral
|
202 | | BashAssocLiteral(Token left, List[AssocPair] pairs, Token right)
|
203 | | Literal %Token
|
204 | # escaped case is separate so the evaluator doesn't have to check token ID
|
205 | | EscapedLiteral(Token token, str ch)
|
206 | | SingleQuoted %SingleQuoted
|
207 | | DoubleQuoted %DoubleQuoted
|
208 | # Could be SimpleVarSub %Token that's VSub_DollarName, but let's not
|
209 | # confuse with the comon word_part.Literal is common for wno
|
210 | | SimpleVarSub %SimpleVarSub
|
211 | | BracedVarSub %BracedVarSub
|
212 | | ZshVarSub (Token left, CompoundWord ignored, Token right)
|
213 | # For command sub and process sub: $(...) <(...) >(...)
|
214 | | CommandSub %CommandSub
|
215 | # ~ or ~bob
|
216 | | TildeSub(Token left, # always the tilde
|
217 | Token? name, str? user_name)
|
218 | | ArithSub(Token left, arith_expr anode, Token right)
|
219 | # {a,b,c}
|
220 | | BracedTuple(List[CompoundWord] words)
|
221 | # {1..10} or {-5..10..2} or {01..10} (leading zeros matter)
|
222 | # {a..f} or {a..f..2} or {a..f..-2}
|
223 | # the whole range is one Token,
|
224 | | BracedRange(Token blame_tok, id kind, str start, str end, int step)
|
225 | # extended globs are parsed statically, unlike globs
|
226 | | ExtGlob(Token op, List[CompoundWord] arms, Token right)
|
227 | # a regex group is similar to an extended glob part
|
228 | | BashRegexGroup(Token left, CompoundWord? child, Token right)
|
229 |
|
230 | # YSH word_part extensions
|
231 |
|
232 | # @myarray - Id.Lit_Splice (could be optimized to %Token)
|
233 | | Splice(Token blame_tok, str var_name)
|
234 | # $[d.key], etc.
|
235 | | ExprSub(Token left, expr child, Token right)
|
236 |
|
237 | # Use cases for Empty: RHS of 'x=', the argument in "${x:-}".
|
238 | # The latter is semantically necessary. (See osh/word_parse.py).
|
239 | # At runtime: RHS of 'declare x='.
|
240 | rhs_word = Empty | Compound %CompoundWord
|
241 |
|
242 | word =
|
243 | # Returns from WordParser, but not generally stored in LST
|
244 | Operator %Token
|
245 | # A Compound word can contain any word_part except the Braced*Part.
|
246 | # We could model this with another variant type but it incurs runtime
|
247 | # overhead and seems like overkill. Note that DoubleQuoted can't
|
248 | # contain a SingleQuoted, etc. either.
|
249 | | Compound %CompoundWord
|
250 | # For word sequences command.Simple, ShArrayLiteral, for_iter.Words
|
251 | # Could be its own type
|
252 | | BracedTree(List[word_part] parts)
|
253 | # For dynamic parsing of test aka [ - the string is already evaluated.
|
254 | | String(id id, str s, CompoundWord? blame_loc)
|
255 |
|
256 | # Note: the name 'foo' is derived from token value 'foo=' or 'foo+='
|
257 | sh_lhs =
|
258 | Name(Token left, str name) # Lit_VarLike foo=
|
259 | # TODO: Could be Name %Token
|
260 | | IndexedName(Token left, str name, arith_expr index)
|
261 | | UnparsedIndex(Token left, str name, str index) # for translation
|
262 |
|
263 | arith_expr =
|
264 | EmptyZero # these are valid: $(( )) (( )) ${a[@]: : }
|
265 | | EmptyOne # condition is 1 for infinite loop: for (( ; ; ))
|
266 | | VarSub %Token # e.g. $(( x )) Id.Arith_VarLike
|
267 | | Word %CompoundWord # e.g. $(( 123'456'$y ))
|
268 |
|
269 | | UnaryAssign(id op_id, arith_expr child)
|
270 | | BinaryAssign(id op_id, arith_expr left, arith_expr right)
|
271 |
|
272 | | Unary(id op_id, arith_expr child)
|
273 | | Binary(Token op, arith_expr left, arith_expr right)
|
274 | | TernaryOp(arith_expr cond, arith_expr true_expr, arith_expr false_expr)
|
275 |
|
276 | bool_expr =
|
277 | WordTest(word w) # e.g. [[ myword ]]
|
278 | | Binary(id op_id, word left, word right)
|
279 | | Unary(id op_id, word child)
|
280 | | LogicalNot(bool_expr child)
|
281 | | LogicalAnd(bool_expr left, bool_expr right)
|
282 | | LogicalOr(bool_expr left, bool_expr right)
|
283 |
|
284 | redir_loc =
|
285 | Fd(int fd) | VarName(str name)
|
286 |
|
287 | redir_param =
|
288 | Word %CompoundWord
|
289 | | HereWord(CompoundWord w, bool is_multiline)
|
290 | | HereDoc(word here_begin, # e.g. EOF or 'EOF'
|
291 | Token? here_end_tok, # Token consisting of the whole line
|
292 | # It's always filled in AFTER creation, but
|
293 | # temporarily so optional
|
294 | List[word_part] stdin_parts # one for each line
|
295 | )
|
296 |
|
297 | Redir = (Token op, redir_loc loc, redir_param arg)
|
298 |
|
299 | assign_op = Equal | PlusEqual
|
300 | AssignPair = (Token left, sh_lhs lhs, assign_op op, rhs_word rhs)
|
301 | # TODO: could put Id.Lit_VarLike foo= into LazyStr() with -1 slice
|
302 | EnvPair = (Token left, str name, rhs_word val)
|
303 |
|
304 | List_of_command < List[command]
|
305 |
|
306 | condition =
|
307 | Shell %List_of_command # if false; true; then echo hi; fi
|
308 | | YshExpr(expr e) # if (x > 0) { echo hi }
|
309 | # TODO: add more specific blame location
|
310 |
|
311 | # Each arm tests one word against multiple words
|
312 | # shell: *.cc|*.h) echo C++ ;;
|
313 | # YSH: *.cc|*.h { echo C++ }
|
314 | #
|
315 | # Three location tokens:
|
316 | # 1. left - shell has ( or *.cc ysh has *.cc
|
317 | # 2. middle - shell has ) ysh has {
|
318 | # 3. right - shell has optional ;; ysh has required }
|
319 | #
|
320 | # For YSH typed case, left can be ( and /
|
321 | # And case_pat may contain more details
|
322 | CaseArm = (
|
323 | Token left, pat pattern, Token middle, List[command] action,
|
324 | Token? right
|
325 | )
|
326 |
|
327 | # The argument to match against in a case command
|
328 | # In YSH-style case commands we match against an `expr`, but in sh-style case
|
329 | # commands we match against a word.
|
330 | case_arg =
|
331 | Word(word w)
|
332 | | YshExpr(expr e)
|
333 |
|
334 | EggexFlag = (bool negated, Token flag)
|
335 |
|
336 | # canonical_flags can be compared for equality. This is needed to splice
|
337 | # eggexes correctly, e.g. / 'abc' @pat ; i /
|
338 | Eggex = (
|
339 | Token left, re regex, List[EggexFlag] flags, Token? trans_pref,
|
340 | str? canonical_flags)
|
341 |
|
342 | pat =
|
343 | Else
|
344 | | Words(List[word] words)
|
345 | | YshExprs(List[expr] exprs)
|
346 | | Eggex %Eggex
|
347 |
|
348 | # Each if arm starts with either an "if" or "elif" keyword
|
349 | # In YSH, the then keyword is not used (replaced by braces {})
|
350 | IfArm = (
|
351 | Token keyword, condition cond, Token? then_kw, List[command] action,
|
352 | # then_tok used in ysh-ify
|
353 | Token? then_tok)
|
354 |
|
355 | for_iter =
|
356 | Args # for x; do echo $x; done # implicit "$@"
|
357 | | Words(List[word] words) # for x in 'foo' *.py { echo $x }
|
358 | # like ShArrayLiteral, but no location for %(
|
359 | | YshExpr(expr e, Token blame) # for x in (mylist) { echo $x }
|
360 | #| Files(Token left, List[word] words)
|
361 | # for x in <> {
|
362 | # for x in < @myfiles > {
|
363 |
|
364 | BraceGroup = (
|
365 | Token left, Token? doc_token, List[command] children, Token right
|
366 | )
|
367 |
|
368 | Param = (Token blame_tok, str name, TypeExpr? type, expr? default_val)
|
369 | RestParam = (Token blame_tok, str name)
|
370 |
|
371 | ParamGroup = (List[Param] params, RestParam? rest_of)
|
372 |
|
373 | # 'open' is for proc p { }; closed is for proc p () { }
|
374 | proc_sig =
|
375 | Open
|
376 | | Closed(ParamGroup? word, ParamGroup? positional, ParamGroup? named,
|
377 | Param? block_param)
|
378 |
|
379 | Proc = (Token keyword, Token name, proc_sig sig, command body)
|
380 |
|
381 | Func = (
|
382 | Token keyword, Token name,
|
383 | ParamGroup? positional, ParamGroup? named,
|
384 | command body
|
385 | )
|
386 |
|
387 | # Represents all these case: s=1 s+=1 s[x]=1 ...
|
388 | ParsedAssignment = (Token? left, Token? close, int part_offset, CompoundWord w)
|
389 |
|
390 | command =
|
391 | NoOp
|
392 |
|
393 | # can wrap many children, e.g. { }, loops, functions
|
394 | | Redirect(command child, List[Redir] redirects)
|
395 |
|
396 | | Simple(Token? blame_tok, # TODO: make required (BracedTuple?)
|
397 | List[EnvPair] more_env,
|
398 | List[word] words,
|
399 | ArgList? typed_args, LiteralBlock? block,
|
400 | # is_last_cmd is used for fork() optimizations
|
401 | bool is_last_cmd)
|
402 |
|
403 | # This doesn't technically belong in the LST, but it's convenient for
|
404 | # execution
|
405 | | ExpandedAlias(command child, List[EnvPair] more_env)
|
406 | | Sentence(command child, Token terminator)
|
407 | # Represents "bare assignment"
|
408 | # Token left is redundant with pairs[0].left
|
409 | | ShAssignment(Token left, List[AssignPair] pairs)
|
410 |
|
411 | | ControlFlow(Token keyword, word? arg_word)
|
412 |
|
413 | # ops are | |&
|
414 | | Pipeline(Token? negated, List[command] children, List[Token] ops)
|
415 | # ops are && ||
|
416 | | AndOr(List[command] children, List[Token] ops)
|
417 |
|
418 | # Part of for, while, until (but not if, case, ShFunction). No redirects.
|
419 | | DoGroup(Token left, List[command] children, Token right)
|
420 | # A brace group is a compound command, with redirects.
|
421 | | BraceGroup %BraceGroup
|
422 | # Contains a single child, like CommandSub
|
423 | | Subshell(Token left, command child, Token right, bool is_last_cmd)
|
424 | | DParen(Token left, arith_expr child, Token right)
|
425 | | DBracket(Token left, bool_expr expr, Token right)
|
426 |
|
427 | # up to 3 iterations variables
|
428 | | ForEach(Token keyword, List[str] iter_names, for_iter iterable,
|
429 | Token? semi_tok, command body)
|
430 | # C-style for loop. Any of the 3 expressions can be omitted.
|
431 | # Note: body is required, but only optional here because of initialization
|
432 | # order.
|
433 | | ForExpr(Token keyword, arith_expr? init, arith_expr? cond,
|
434 | arith_expr? update, command? body)
|
435 | | WhileUntil(Token keyword, condition cond, command body)
|
436 |
|
437 | | If(Token if_kw, List[IfArm] arms, Token? else_kw, List[command] else_action,
|
438 | Token? fi_kw)
|
439 | | Case(Token case_kw, case_arg to_match, Token arms_start, List[CaseArm] arms,
|
440 | Token arms_end)
|
441 |
|
442 | # The keyword is optional in the case of bash-style functions
|
443 | # (ie. "foo() { ... }") which do not have one.
|
444 | | ShFunction(Token? keyword, Token name_tok, str name, command body)
|
445 |
|
446 | | TimeBlock(Token keyword, command pipeline)
|
447 | # Some nodes optimize it out as List[command], but we use CommandList for
|
448 | # 1. the top level
|
449 | # 2. ls ; ls & ls (same line)
|
450 | # 3. CommandSub # single child that's a CommandList
|
451 | # 4. Subshell # single child that's a CommandList
|
452 |
|
453 | # TODO: Use List_of_command
|
454 | | CommandList(List[command] children)
|
455 |
|
456 | # YSH command constructs
|
457 |
|
458 | # var, const.
|
459 | # - Keyword is None for hay blocks
|
460 | # - RHS is None, for use with value.Place
|
461 | # - TODO: consider using BareDecl
|
462 | | VarDecl(Token? keyword, List[NameType] lhs, expr? rhs)
|
463 |
|
464 | # this can behave like 'var', can be desugared
|
465 | | BareDecl(Token lhs, expr rhs)
|
466 |
|
467 | # setvar, maybe 'auto' later
|
468 | | Mutation(Token keyword, List[y_lhs] lhs, Token op, expr rhs)
|
469 | # = keyword
|
470 | | Expr(Token keyword, expr e)
|
471 | | Proc %Proc
|
472 | | Func %Func
|
473 | | Retval(Token keyword, expr val)
|
474 |
|
475 | #
|
476 | # Glob representation, for converting ${x//} to extended regexes.
|
477 | #
|
478 |
|
479 | # Example: *.[ch] is:
|
480 | # GlobOp(<Glob_Star '*'>),
|
481 | # GlobLit(Glob_OtherLiteral, '.'),
|
482 | # CharClass(False, ['ch']) # from Glob_CleanLiterals token
|
483 |
|
484 | glob_part =
|
485 | Literal(id id, str s)
|
486 | | Operator(id op_id) # * or ?
|
487 | | CharClass(bool negated, List[str] strs)
|
488 |
|
489 | # Char classes are opaque for now. If we ever need them:
|
490 | # - Collating symbols are [. .]
|
491 | # - Equivalence classes are [=
|
492 |
|
493 | printf_part =
|
494 | Literal %Token
|
495 | # flags are 0 hyphen space + #
|
496 | # type is 's' for %s, etc.
|
497 | | Percent(List[Token] flags, Token? width, Token? precision, Token type)
|
498 |
|
499 | #
|
500 | # YSH Language
|
501 | #
|
502 | # Copied and modified from Python-3.7/Parser/Python.asdl !
|
503 |
|
504 | expr_context = Load | Store | Del | AugLoad | AugStore | Param
|
505 |
|
506 | # Type expressions: Int List[Int] Dict[Str, Any]
|
507 | # Do we have Func[Int, Int => Int] ? I guess we can parse that into this
|
508 | # system.
|
509 | TypeExpr = (Token tok, str name, List[TypeExpr] params)
|
510 |
|
511 | # LHS bindings in var/const, and eggex
|
512 | NameType = (Token left, str name, TypeExpr? typ)
|
513 |
|
514 | # TODO: Inline this into GenExp and ListComp? Just use a flag there?
|
515 | Comprehension = (List[NameType] lhs, expr iter, expr? cond)
|
516 |
|
517 | # Named arguments supplied to call. Token is null for f(; ...named).
|
518 | NamedArg = (Token? name, expr value)
|
519 |
|
520 | # Subscripts are lists of expressions
|
521 | # a[:i, n] (we don't have matrices, but we have data frames)
|
522 | Subscript = (Token left, expr obj, expr index)
|
523 |
|
524 | # Attributes are obj.attr, d->key, name::scope,
|
525 | Attribute = (expr obj, Token op, Token attr, str attr_name, expr_context ctx)
|
526 |
|
527 | y_lhs =
|
528 | Var %Token # Id.Expr_Name
|
529 | | Subscript %Subscript
|
530 | | Attribute %Attribute
|
531 |
|
532 | place_op =
|
533 | # &a[i+1]
|
534 | Subscript(Token op, expr index)
|
535 | # &d.mykey
|
536 | | Attribute(Token op, Token attr)
|
537 |
|
538 | expr =
|
539 | Var(Token left, str name) # a variable name to evaluate
|
540 | # Constants are typically Null, Bool, Int, Float
|
541 | # and also Str for key in {key: 42}
|
542 | # But string literals are SingleQuoted or DoubleQuoted
|
543 | # Python uses Num(object n), which doesn't respect our "LST" invariant.
|
544 | | Const(Token c, value val)
|
545 |
|
546 | # read(&x) json read (&x[0])
|
547 | | Place(Token blame_tok, str var_name, place_op* ops)
|
548 |
|
549 | # :| one 'two' "$three" |
|
550 | | ShArrayLiteral %ShArrayLiteral
|
551 |
|
552 | # / d+ ; ignorecase; %python /
|
553 | | Eggex %Eggex
|
554 |
|
555 | # $name is not an expr, but $? is, e.g. Id.VSub_QMark
|
556 | | SimpleVarSub %SimpleVarSub
|
557 | | BracedVarSub %BracedVarSub
|
558 | | CommandSub %CommandSub
|
559 | | SingleQuoted %SingleQuoted
|
560 | | DoubleQuoted %DoubleQuoted
|
561 |
|
562 | | Literal(expr inner)
|
563 | | Lambda(List[NameType] params, expr body)
|
564 |
|
565 | | Unary(Token op, expr child)
|
566 | | Binary(Token op, expr left, expr right)
|
567 | # x < 4 < 3 and (x < 4) < 3
|
568 | | Compare(expr left, List[Token] ops, List[expr] comparators)
|
569 | | FuncCall(expr func, ArgList args)
|
570 |
|
571 | # TODO: Need a representation for method call. We don't just want
|
572 | # Attribute() and then Call()
|
573 |
|
574 | | IfExp(expr test, expr body, expr orelse)
|
575 | | Tuple(Token left, List[expr] elts, expr_context ctx)
|
576 |
|
577 | | List(Token left, List[expr] elts, expr_context ctx)
|
578 | | Dict(Token left, List[expr] keys, List[expr] values)
|
579 | # For the values in {n1, n2}
|
580 | | Implicit
|
581 |
|
582 | | ListComp(Token left, expr elt, List[Comprehension] generators)
|
583 | # not implemented
|
584 | | DictComp(Token left, expr key, expr value, List[Comprehension] generators)
|
585 | | GeneratorExp(expr elt, List[Comprehension] generators)
|
586 |
|
587 | # Ranges are written 1:2, with first class expression syntax. There is no
|
588 | # step as in Python. Use range(0, 10, step=2) for that.
|
589 | | Range(expr lower, Token op, expr upper)
|
590 |
|
591 | # Slices occur within [] only. Unlike ranges, the start/end can be #
|
592 | # implicit. Like ranges, denote a step with slice(0, 10, step=2).
|
593 | # a[3:] a[:i]
|
594 | | Slice(expr? lower, Token op, expr? upper)
|
595 |
|
596 | | Subscript %Subscript
|
597 | | Attribute %Attribute
|
598 |
|
599 | # Ellipsis is like 'Starred' within Python, which are valid on the LHS in
|
600 | # Python for unpacking, and # within list literals for splicing.
|
601 | # (Starred is NOT used for {k:v, **a}. That used a blank "keys"
|
602 | # attribute.)
|
603 |
|
604 | # I think we can use { **pairs } like Python
|
605 | | Spread(Token left, expr child)
|
606 |
|
607 | #
|
608 | # Regex Language (Eggex)
|
609 | #
|
610 |
|
611 | # e.g. alnum digit
|
612 | PosixClass = (Token? negated, str name)
|
613 | # e.g. d w s
|
614 | PerlClass = (Token? negated, str name)
|
615 |
|
616 | # Char Sets and Ranges both use Char Codes
|
617 | # with u_braced == true : \u{ff}
|
618 | # with u_braced == false: \xff \\ 'a' a '0' 0
|
619 | # ERE doesn't make a distinction, but compiling to Python/PCRE can use it
|
620 | CharCode = (Token blame_tok, int i, bool u_braced)
|
621 | CharRange = (CharCode start, CharCode end)
|
622 |
|
623 | # Note: .NET has && in character classes, making it a recursive language
|
624 |
|
625 | class_literal_term =
|
626 | PosixClass %PosixClass
|
627 | | PerlClass %PerlClass
|
628 | | CharRange %CharRange
|
629 | | CharCode %CharCode
|
630 |
|
631 | | SingleQuoted %SingleQuoted
|
632 | # @chars
|
633 | | Splice(Token name, str var_name) # coudl be Splice %Token
|
634 |
|
635 | # evaluated version of class_literal_term (could be in runtime.asdl)
|
636 | char_class_term =
|
637 | PosixClass %PosixClass
|
638 | | PerlClass %PerlClass
|
639 |
|
640 | | CharRange %CharRange
|
641 | # For [ \x00 \\ ]
|
642 | | CharCode %CharCode
|
643 |
|
644 | # NOTE: modifier is unused now, can represent L or P
|
645 | re_repeat =
|
646 | Op %Token # + * ? or Expr_DecInt for x{3}
|
647 | | Range(Token? left, str lower, str upper, Token? right) # dot{1,2}
|
648 | # Haven't implemented the modifier, e.g. x{+ P}
|
649 | # | Num(Token times, id modifier)
|
650 | # | Range(Token? lower, Token? upper, id modifier)
|
651 |
|
652 | re =
|
653 | Primitive(Token blame_tok, id id) # . ^ $ dot %start %end
|
654 | | PosixClass %PosixClass
|
655 | | PerlClass %PerlClass
|
656 | # syntax [ $x \n ]
|
657 | | CharClassLiteral(bool negated, List[class_literal_term] terms)
|
658 | # evaluated [ 'abc' \n ]
|
659 | | CharClass(bool negated, List[char_class_term] terms)
|
660 |
|
661 | # @D
|
662 | | Splice(Token name, str var_name) # TODO: Splice %Token ?
|
663 |
|
664 | | SingleQuoted %SingleQuoted
|
665 |
|
666 | # Compound:
|
667 | | Repeat(re child, re_repeat op)
|
668 | | Seq(List[re] children)
|
669 | | Alt(List[re] children)
|
670 |
|
671 | | Group(re child)
|
672 | # convert_func is filled in on evaluation
|
673 | # TODO: name and func_name can be expanded to strings
|
674 | | Capture(re child, Token? name, Token? func_name)
|
675 | | Backtracking(bool negated, Token name, re child)
|
676 |
|
677 | # \u{ff} is parsed as this, but SingleQuoted also evaluates to it
|
678 | | LiteralChars(Token blame_tok, str s)
|
679 | }
|