OILS / frontend / syntax.asdl View on Github | oilshell.org

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