OILS / frontend / syntax.asdl View on Github | oils.pub

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