OILS / frontend / id_kind_def.py View on Github | oilshell.org

796 lines, 541 significant
1#!/usr/bin/env python2
2# Copyright 2016 Andy Chu. All rights reserved.
3# Licensed under the Apache License, Version 2.0 (the "License");
4# you may not use this file except in compliance with the License.
5# You may obtain a copy of the License at
6#
7# http://www.apache.org/licenses/LICENSE-2.0
8"""
9id_kind_def.py - Id and Kind definitions, stored in Token
10
11NOTE: If this file changes, rebuild it with build/py.sh all
12"""
13from __future__ import print_function
14
15from _devbuild.gen.types_asdl import (bool_arg_type_e, bool_arg_type_t)
16#from mycpp.mylib import log
17
18from typing import List, Tuple, Dict, Optional, TYPE_CHECKING
19if TYPE_CHECKING: # avoid circular build deps
20 from _devbuild.gen.id_kind_asdl import Id_t, Kind_t
21
22
23class IdSpec(object):
24 """Identifiers that form the "spine" of the shell program
25 representation."""
26
27 def __init__(self, kind_lookup, bool_ops):
28 # type: (Dict[int, int], Dict[int, bool_arg_type_t]) -> None
29 self.id_str2int = {} # type: Dict[str, int]
30 self.kind_str2int = {} # type: Dict[str, int]
31
32 self.kind_lookup = kind_lookup # Id int -> Kind int
33 self.kind_name_list = [] # type: List[str]
34 self.kind_sizes = [] # type: List[int] # optional stats
35
36 self.lexer_pairs = {} # type: Dict[int, List[Tuple[bool, str, int]]]
37 self.bool_ops = bool_ops # type: Dict[int, bool_arg_type_t]
38
39 # Incremented on each method call
40 # IMPORTANT: 1-based indices match what asdl/gen_python.py does!!!
41 self.id_index = 1
42 self.kind_index = 1
43
44 def LexerPairs(self, kind):
45 # type: (Kind_t) -> List[Tuple[bool, str, Id_t]]
46 result = []
47 for is_regex, pat, id_ in self.lexer_pairs[kind]:
48 result.append((is_regex, pat, id_))
49 return result
50
51 def _AddId(self, id_name, kind=None):
52 # type: (str, Optional[int]) -> int
53 """
54 Args:
55 id_name: e.g. BoolBinary_Equal
56 kind: override autoassignment. For AddBoolBinaryForBuiltin
57 """
58 t = self.id_index
59
60 self.id_str2int[id_name] = t
61
62 if kind is None:
63 kind = self.kind_index
64 self.kind_lookup[t] = kind
65
66 self.id_index += 1 # mutate last
67 return t # the index we used
68
69 def _AddKind(self, kind_name):
70 # type: (str) -> None
71 self.kind_str2int[kind_name] = self.kind_index
72 #log('%s = %d', kind_name, self.kind_index)
73 self.kind_index += 1
74 self.kind_name_list.append(kind_name)
75
76 def AddKind(self, kind_name, tokens):
77 # type: (str, List[str]) -> None
78 assert isinstance(tokens, list), tokens
79
80 for name in tokens:
81 id_name = '%s_%s' % (kind_name, name)
82 self._AddId(id_name)
83
84 # Must be after adding Id
85 self._AddKind(kind_name)
86 self.kind_sizes.append(len(tokens)) # debug info
87
88 def AddKindPairs(self, kind_name, pairs):
89 # type: (str, List[Tuple[str, str]]) -> None
90 assert isinstance(pairs, list), pairs
91
92 lexer_pairs = []
93 for name, char_pat in pairs:
94 id_name = '%s_%s' % (kind_name, name)
95 id_int = self._AddId(id_name)
96 # After _AddId
97 lexer_pairs.append((False, char_pat, id_int)) # Constant
98
99 self.lexer_pairs[self.kind_index] = lexer_pairs
100
101 # Must be after adding Id
102 self._AddKind(kind_name)
103 self.kind_sizes.append(len(pairs)) # debug info
104
105 def AddBoolKind(
106 self,
107 kind_name, # type: str
108 arg_type_pairs, # type: List[Tuple[bool_arg_type_t, List[Tuple[str, str]]]]
109 ):
110 # type: (...) -> None
111 """
112 Args:
113 kind_name: string
114 arg_type_pairs: dictionary of bool_arg_type_e -> []
115 """
116 lexer_pairs = []
117 num_tokens = 0
118 for arg_type, pairs in arg_type_pairs:
119 #print(arg_type, pairs)
120
121 for name, char_pat in pairs:
122 # BoolUnary_f, BoolBinary_eq, BoolBinary_NEqual
123 id_name = '%s_%s' % (kind_name, name)
124 id_int = self._AddId(id_name)
125 self.AddBoolOp(id_int, arg_type) # register type
126 lexer_pairs.append((False, char_pat, id_int)) # constant
127
128 num_tokens += len(pairs)
129
130 self.lexer_pairs[self.kind_index] = lexer_pairs
131
132 # Must do this after _AddId()
133 self._AddKind(kind_name)
134 self.kind_sizes.append(num_tokens) # debug info
135
136 def AddBoolBinaryForBuiltin(self, id_name, kind):
137 # type: (str, int) -> int
138 """For [ = ] [ == ] and [ != ].
139
140 These operators are NOT added to the lexer. The are "lexed" as
141 word.String.
142 """
143 id_name = 'BoolBinary_%s' % id_name
144 id_int = self._AddId(id_name, kind=kind)
145 self.AddBoolOp(id_int, bool_arg_type_e.Str)
146 return id_int
147
148 def AddBoolOp(self, id_int, arg_type):
149 # type: (int, bool_arg_type_t) -> None
150 """Associate an ID integer with an bool_arg_type_e."""
151 self.bool_ops[id_int] = arg_type
152
153
154def AddKinds(spec):
155 # type: (IdSpec) -> None
156
157 # A compound word, in arith context, boolean context, or command context.
158 # A['foo'] A["foo"] A[$foo] A["$foo"] A[${foo}] A["${foo}"]
159 spec.AddKind('Word', ['Compound'])
160
161 # Token IDs in Kind.Arith are first to make the TDOP precedence table
162 # small.
163 #
164 # NOTE: Could share Op_Pipe, Op_Amp, Op_DAmp, Op_Semi, Op_LParen, etc.
165 # Actually all of Arith could be folded into Op, because we are using
166 # WordParser._ReadArithWord vs. WordParser._ReadWord.
167 spec.AddKindPairs(
168 'Arith',
169 [
170 ('Semi', ';'), # ternary for loop only
171 ('Comma', ','), # function call and C comma operator
172 ('Plus', '+'),
173 ('Minus', '-'),
174 ('Star', '*'),
175 ('Slash', '/'),
176 ('Percent', '%'),
177 ('DPlus', '++'),
178 ('DMinus', '--'),
179 ('DStar', '**'),
180 ('LParen', '('),
181 ('RParen', ')'), # grouping and function call extension
182 ('LBracket', '['),
183 ('RBracket', ']'), # array and assoc array subscript
184 ('RBrace', '}'), # for end of var sub
185
186 # Logical Ops
187 ('QMark', '?'),
188 ('Colon', ':'), # Ternary Op: a < b ? 0 : 1
189 ('LessEqual', '<='),
190 ('Less', '<'),
191 ('GreatEqual', '>='),
192 ('Great', '>'),
193 ('DEqual', '=='),
194 ('NEqual', '!='),
195 # note: these 3 are not in YSH Expr. (Could be used in find dialect.)
196 ('DAmp', '&&'),
197 ('DPipe', '||'),
198 ('Bang', '!'),
199
200 # Bitwise ops
201 ('DGreat', '>>'),
202 ('DLess', '<<'),
203 # YSH: ^ is exponent
204 ('Amp', '&'),
205 ('Pipe', '|'),
206 ('Caret', '^'),
207 ('Tilde', '~'),
208 ('Equal', '='),
209
210 # Augmented Assignment for $(( ))
211 # Must match the list in osh/arith_parse.py
212 # YSH has **= //= like Python
213 ('PlusEqual', '+='),
214 ('MinusEqual', '-='),
215 ('StarEqual', '*='),
216 ('SlashEqual', '/='),
217 ('PercentEqual', '%='),
218 ('DGreatEqual', '>>='),
219 ('DLessEqual', '<<='),
220 ('AmpEqual', '&='),
221 ('CaretEqual', '^='),
222 ('PipeEqual', '|='),
223 ])
224
225 spec.AddKind('Eof', ['Real', 'RParen', 'Backtick'])
226
227 spec.AddKind('Undefined', ['Tok']) # for initial state
228
229 # The Unknown kind is used when we lex something, but it's invalid.
230 # Examples:
231 # ${^}
232 # $'\z' Such bad codes are accepted when parse_backslash is on
233 # (default in OSH), so we have to lex them.
234 # (x == y) should used === or ~==
235 spec.AddKind('Unknown', ['Tok', 'Backslash', 'DEqual'])
236
237 spec.AddKind('Eol', ['Tok']) # no more tokens on line (\0)
238
239 # Ignored_Newline is for J8 lexing to count lines
240 spec.AddKind('Ignored', ['LineCont', 'Space', 'Comment', 'Newline'])
241
242 # Id.WS_Space is for lex_mode_e.ShCommand; Id.Ignored_Space is for
243 # lex_mode_e.Arith
244 spec.AddKind('WS', ['Space'])
245
246 spec.AddKind(
247 'Lit',
248 [
249 'Chars',
250 'CharsWithoutPrefix', # for stripping leading whitespace
251 'VarLike',
252 'ArrayLhsOpen',
253 'ArrayLhsClose',
254 'Splice', # @func(a, b)
255 'AtLBracket', # @[split(x)]
256 'AtLBraceDot', # @{.myproc arg1} should be builtin_sub
257 'Other',
258 'EscapedChar', # \* is escaped
259 'LBracket',
260 'RBracket', # for assoc array literals, static globs
261 'Star',
262 'QMark',
263 # Either brace expansion or keyword for { and }
264 'LBrace',
265 'RBrace',
266 'Comma',
267 'Equals', # For = f()
268 'Dollar', # detecting 'echo $'
269 'DRightBracket', # the ]] that matches [[, NOT a keyword
270 'Tilde', # tilde expansion
271 'Pound', # for comment or VarOp state
272 'TPound', # for doc comments like ###
273 'TDot', # for multiline commands ...
274 'Slash',
275 'Percent', # / # % for patsub, NOT unary op
276 'Colon', # x=foo:~:~root needs tilde expansion
277 'Digits', # for lex_mode_e.Arith
278 'At', # for ${a[@]} in lex_mode_e.Arith, and detecting @[]
279 'ArithVarLike', # for $((var+1)). Distinct from Lit_VarLike 'var='
280 'BadBackslash', # for "\z", not Id.Unknown_Backslash because it's a
281 # syntax error in YSH, but NOT OSH
282 'CompDummy', # A fake Lit_* token to get partial words during
283 # completion
284 ])
285
286 # For recognizing \` and \" and \\ within backticks. There's an extra layer
287 # of backslash quoting.
288 spec.AddKind('Backtick', ['Right', 'Quoted', 'DoubleQuote', 'Other'])
289
290 spec.AddKind('History', ['Op', 'Num', 'Search', 'Other'])
291
292 spec.AddKind(
293 'Op',
294 [
295 'Newline', # mostly equivalent to SEMI
296 'Amp', # &
297 'Pipe', # |
298 'PipeAmp', # |& -- bash extension for stderr
299 'DAmp', # &&
300 'DPipe', # ||
301 'Semi', # ;
302 'DSemi', # ;; for case
303 'SemiAmp', # ;& for case
304 'DSemiAmp', # ;;& for case
305 'LParen', # For subshell. Not Kind.Left because it's NOT a WordPart.
306 'RParen', # Default, will be translated to Id.Right_*
307 'DLeftParen',
308 'DRightParen',
309
310 # for [[ ]] language
311 'Less', # <
312 'Great', # >
313 'Bang', # !
314
315 # YSH [] {}
316 'LBracket',
317 'RBracket',
318 'LBrace',
319 'RBrace',
320 ])
321
322 # YSH expressions use Kind.Expr and Kind.Arith (further below)
323 spec.AddKind(
324 'Expr',
325 [
326 'Reserved', # <- means nothing but it's reserved now
327 'Symbol', # %foo
328 'Name',
329 'DecInt',
330 'BinInt',
331 'OctInt',
332 'HexInt',
333 'Float',
334 'Bang', # eggex !digit, ![a-z]
335 'Dot',
336 'DDot',
337 'Colon', # mylist:pop()
338 'RArrow',
339 'RDArrow',
340 'DSlash', # integer division
341 'TEqual',
342 'NotDEqual',
343 'TildeDEqual', # === !== ~==
344 'At',
345 'DoubleAt', # splice operators
346 'Ellipsis', # for varargs
347 'Dollar', # legacy regex
348 'NotTilde', # !~
349 'DTilde',
350 'NotDTilde', # ~~ !~~
351 'DStarEqual', # **=, which bash doesn't have
352 'DSlashEqual', # //=, which bash doesn't have
353 'CastedDummy', # Used for @() $() (words in lex_mode_e.ShCommand)
354 # and ${} '' "" (and all other strings)
355
356 # Constants
357 'Null',
358 'True',
359 'False',
360
361 # Keywords are resolved after lexing, but otherwise behave like tokens.
362 'And',
363 'Or',
364 'Not',
365
366 # List comprehensions
367 'For',
368 'Is',
369 'In',
370 'If',
371 'Else',
372 'Func', # For function literals
373 'Capture',
374 'As',
375 ])
376
377 # For C-escaped strings.
378 spec.AddKind(
379 'Char',
380 [
381 'OneChar',
382 'Stop',
383 'Hex', # \xff
384 'YHex', # \yff for J8 notation
385
386 # Two variants of Octal: \377, and \0377.
387 'Octal3',
388 'Octal4',
389 'Unicode4',
390 'SurrogatePair', # JSON
391 'Unicode8', # bash
392 'UBraced',
393 'Pound', # YSH
394 'AsciiControl', # \x01-\x1f, what's disallowed in JSON
395 ])
396
397 # For lex_mode_e.BashRegex
398 # Bash treats ( | ) as special, and space is allowed within ()
399 # Note Id.Op_RParen -> Id.Right_BashRegex with lexer hint
400 spec.AddKind('BashRegex', ['LParen', 'AllowedInParens'])
401
402 spec.AddKind(
403 'Eggex',
404 [
405 'Start', # ^ or %start
406 'End', # $ or %end
407 'Dot', # . or dot
408 # Future: %boundary generates \b in Python/Perl, etc.
409 ])
410
411 spec.AddKind(
412 'Redir',
413 [
414 'Less', # < stdin
415 'Great', # > stdout
416 'DLess', # << here doc redirect
417 'TLess', # <<< bash only here string
418 'DGreat', # >> append stdout
419 'GreatAnd', # >& descriptor redirect
420 'LessAnd', # <& descriptor redirect
421 'DLessDash', # <<- here doc redirect for tabs?
422 'LessGreat', # <>
423 'Clobber', # >| POSIX?
424 'AndGreat', # bash &> stdout/stderr to file
425 'AndDGreat', # bash &>> stdout/stderr append to file
426
427 #'GreatPlus', # >+ is append in YSH
428 #'DGreatPlus', # >>+ is append to string in YSH
429 ])
430
431 # NOTE: This is for left/right WORDS only. (( is not a word so it doesn't
432 # get that.
433 spec.AddKind(
434 'Left',
435 [
436 'DoubleQuote',
437 'SingleQuote', # ''
438 'DollarSingleQuote', # $'' for \n escapes
439 'RSingleQuote', # r''
440 'USingleQuote', # u''
441 'BSingleQuote', # b''
442
443 # Multiline versions
444 'TDoubleQuote', # """ """
445 'TSingleQuote', # ''' '''
446 'RTSingleQuote', # r''' '''
447 'UTSingleQuote', # u''' '''
448 'BTSingleQuote', # b''' '''
449 'Backtick', # `
450 'DollarParen', # $(
451 'DollarBrace', # ${
452 'DollarBraceZsh', # ${(foo)
453 'DollarDParen', # $((
454 'DollarBracket', # $[ - synonym for $(( in bash and zsh
455 'DollarDoubleQuote', # $" for bash localized strings
456 'ProcSubIn', # <( )
457 'ProcSubOut', # >( )
458 'AtParen', # @( for split command sub
459 'CaretParen', # ^( for Block literal in expression mode
460 'CaretBracket', # ^[ for Expr literal
461 'CaretBrace', # ^{ for Arglist
462 'CaretDoubleQuote', # ^" for Template
463 'ColonPipe', # :| for word arrays
464 'PercentParen', # legacy %( for word arrays
465 ])
466
467 spec.AddKind(
468 'Right',
469 [
470 'DoubleQuote',
471 'SingleQuote',
472 'Backtick', # `
473 'DollarBrace', # }
474 'DollarDParen', # )) -- really the second one is a PushHint()
475 # ArithSub2 is just Id.Arith_RBracket
476 'DollarDoubleQuote', # "
477 'DollarSingleQuote', # '
478
479 # Disambiguated right parens
480 'Subshell', # )
481 'ShFunction', # )
482 'CasePat', # )
483 'ShArrayLiteral', # )
484 'ExtGlob', # )
485 'BashRegexGroup', # )
486 'BlockLiteral', # } that matches &{ echo hi }
487 ])
488
489 spec.AddKind('ExtGlob', ['Comma', 'At', 'Star', 'Plus', 'QMark', 'Bang'])
490
491 # First position of var sub ${
492 # Id.VOp2_Pound -- however you can't tell the difference at first! It could
493 # be an op or a name. So it makes sense to base i on the state.
494 # Id.VOp2_At
495 # But then you have AS_STAR, or Id.Arith_Star maybe
496
497 spec.AddKind(
498 'VSub',
499 [
500 'DollarName', # $foo
501 'Name', # 'foo' in ${foo}
502 'Number', # $0 .. $9
503 'Bang', # $!
504 'At', # $@ or [@] for array subscripting
505 'Pound', # $# or ${#var} for length
506 'Dollar', # $$
507 'Star', # $*
508 'Hyphen', # $-
509 'QMark', # $?
510 'Dot', # ${.myproc builtin sub}
511 ])
512
513 spec.AddKindPairs('VTest', [
514 ('ColonHyphen', ':-'),
515 ('Hyphen', '-'),
516 ('ColonEquals', ':='),
517 ('Equals', '='),
518 ('ColonQMark', ':?'),
519 ('QMark', '?'),
520 ('ColonPlus', ':+'),
521 ('Plus', '+'),
522 ])
523
524 # Statically parse @P, so @x etc. is an error.
525 spec.AddKindPairs(
526 'VOp0',
527 [
528 ('Q', '@Q'), # ${x@Q} for quoting
529 ('E', '@E'),
530 ('P', '@P'), # ${PS1@P} for prompt eval
531 ('A', '@A'),
532 ('a', '@a'),
533 ])
534
535 # String removal ops
536 spec.AddKindPairs(
537 'VOp1',
538 [
539 ('Percent', '%'),
540 ('DPercent', '%%'),
541 ('Pound', '#'),
542 ('DPound', '##'),
543 # Case ops, in bash. At least parse them. Execution might require
544 # unicode stuff.
545 ('Caret', '^'),
546 ('DCaret', '^^'),
547 ('Comma', ','),
548 ('DComma', ',,'),
549 ])
550
551 spec.AddKindPairs(
552 'VOpYsh',
553 [
554 ('Pipe', '|'), # ${x|html}
555 ('Space', ' '), # ${x %.3f}
556 ])
557
558 # Not in POSIX, but in Bash
559 spec.AddKindPairs(
560 'VOp2',
561 [
562 ('Slash', '/'), # / for replacement
563 ('Colon', ':'), # : for slicing
564 ('LBracket', '['), # [ for indexing
565 ('RBracket', ']'), # ] for indexing
566 ])
567
568 # Can only occur after ${!prefix@}
569 spec.AddKindPairs('VOp3', [
570 ('At', '@'),
571 ('Star', '*'),
572 ])
573
574 # This kind is for Node types that are NOT tokens.
575 spec.AddKind(
576 'Node',
577 [
578 # Arithmetic nodes
579 'PostDPlus',
580 'PostDMinus', # Postfix inc/dec.
581 # Prefix inc/dec use Arith_DPlus/Arith_DMinus.
582 'UnaryPlus',
583 'UnaryMinus', # +1 and -1, to distinguish from infix.
584 # Actually we don't need this because we they
585 # will be under Expr1/Plus vs Expr2/Plus.
586 'NotIn',
587 'IsNot', # For YSH comparisons
588 ])
589
590 # NOTE: Not doing AddKindPairs() here because oil will have a different set
591 # of keywords. It will probably have for/in/while/until/case/if/else/elif,
592 # and then func/proc.
593 spec.AddKind(
594 'KW',
595 [
596 'DLeftBracket',
597 'Bang',
598 'For',
599 'While',
600 'Until',
601 'Do',
602 'Done',
603 'In',
604 'Case',
605 'Esac',
606 'If',
607 'Fi',
608 'Then',
609 'Else',
610 'Elif',
611 'Function',
612 'Time',
613
614 # YSH keywords.
615 'Const',
616 'Var',
617 'SetVar',
618 'SetGlobal',
619 # later: Auto?
620 'Call',
621 'Proc',
622 'Typed',
623 'Func',
624
625 # builtins, NOT keywords: use, fork, wait, etc.
626 # Things that don't affect parsing shouldn't be keywords.
627 ])
628
629 # Unlike bash, we parse control flow statically. They're not
630 # dynamically-resolved builtins.
631 spec.AddKind('ControlFlow', ['Break', 'Continue', 'Return', 'Exit'])
632
633 # Special Kind for lookahead in the lexer. It's never seen by anything else.
634 spec.AddKind('LookAhead', ['FuncParens'])
635
636 # For parsing globs and converting them to regexes.
637 spec.AddKind('Glob', [
638 'LBracket',
639 'RBracket',
640 'Star',
641 'QMark',
642 'Bang',
643 'Caret',
644 'EscapedChar',
645 'BadBackslash',
646 'CleanLiterals',
647 'OtherLiteral',
648 ])
649
650 # For C-escaped strings.
651 spec.AddKind(
652 'Format',
653 [
654 'EscapedPercent',
655 'Percent', # starts another lexer mode
656 'Flag',
657 'Num',
658 'Dot',
659 'Type',
660 'Star',
661 'Time',
662 'Zero',
663 ])
664
665 # For parsing prompt strings like PS1.
666 spec.AddKind('PS', [
667 'Subst',
668 'Octal3',
669 'LBrace',
670 'RBrace',
671 'Literals',
672 'BadBackslash',
673 ])
674
675 spec.AddKind('Range', ['Int', 'Char', 'Dots', 'Other'])
676
677 spec.AddKind(
678 'J8',
679 [
680 'LBracket',
681 'RBracket',
682 'LBrace',
683 'RBrace',
684 'Comma',
685 'Colon',
686 'Null',
687 'Bool',
688 'Int', # Number
689 'Float', # Number
690
691 # High level tokens for "" b'' u''
692 # We don't distinguish them in the parser, because we recognize
693 # strings in the lexer.
694 'String',
695
696 # JSON8 and NIL8
697 'Identifier',
698 'Newline', # J8 Lines only, similar to Op_Newline
699 'Tab', # Reserved for TSV8
700
701 # NIL8 only
702 'LParen',
703 'RParen',
704 #'Symbol',
705 'Operator',
706 ])
707
708
709# Shared between [[ and test/[.
710_UNARY_STR_CHARS = 'zn' # -z -n
711_UNARY_OTHER_CHARS = 'otvR' # -o is overloaded
712_UNARY_PATH_CHARS = 'abcdefghkLprsSuwxOGN' # -a is overloaded
713
714_BINARY_PATH = ['ef', 'nt', 'ot']
715_BINARY_INT = ['eq', 'ne', 'gt', 'ge', 'lt', 'le']
716
717
718def _Dash(strs):
719 # type: (List[str]) -> List[Tuple[str, str]]
720 # Gives a pair of (token name, string to match)
721 return [(s, '-' + s) for s in strs]
722
723
724def AddBoolKinds(spec):
725 # type: (IdSpec) -> None
726 spec.AddBoolKind('BoolUnary', [
727 (bool_arg_type_e.Str, _Dash(list(_UNARY_STR_CHARS))),
728 (bool_arg_type_e.Other, _Dash(list(_UNARY_OTHER_CHARS))),
729 (bool_arg_type_e.Path, _Dash(list(_UNARY_PATH_CHARS))),
730 ])
731
732 spec.AddBoolKind('BoolBinary', [
733 (bool_arg_type_e.Str, [
734 ('GlobEqual', '='),
735 ('GlobDEqual', '=='),
736 ('GlobNEqual', '!='),
737 ('EqualTilde', '=~'),
738 ]),
739 (bool_arg_type_e.Path, _Dash(_BINARY_PATH)),
740 (bool_arg_type_e.Int, _Dash(_BINARY_INT)),
741 ])
742
743 Id = spec.id_str2int
744 # logical, arity, arg_type
745 spec.AddBoolOp(Id['Op_DAmp'], bool_arg_type_e.Undefined)
746 spec.AddBoolOp(Id['Op_DPipe'], bool_arg_type_e.Undefined)
747 spec.AddBoolOp(Id['KW_Bang'], bool_arg_type_e.Undefined)
748
749 spec.AddBoolOp(Id['Op_Less'], bool_arg_type_e.Str)
750 spec.AddBoolOp(Id['Op_Great'], bool_arg_type_e.Str)
751
752
753def SetupTestBuiltin(
754 id_spec, # type: IdSpec
755 unary_lookup, # type: Dict[str, int]
756 binary_lookup, # type: Dict[str, int]
757 other_lookup, # type: Dict[str, int]
758):
759 # type: (...) -> None
760 """Setup tokens for test/[.
761
762 Similar to _AddBoolKinds above. Differences:
763 - =~ doesn't exist
764 - && -> -a, || -> -o
765 - ( ) -> Op_LParen (they don't appear above)
766 """
767 Id = id_spec.id_str2int
768 Kind = id_spec.kind_str2int
769
770 for letter in _UNARY_STR_CHARS + _UNARY_OTHER_CHARS + _UNARY_PATH_CHARS:
771 id_name = 'BoolUnary_%s' % letter
772 unary_lookup['-' + letter] = Id[id_name]
773
774 for s in _BINARY_PATH + _BINARY_INT:
775 id_name = 'BoolBinary_%s' % s
776 binary_lookup['-' + s] = Id[id_name]
777
778 # Like the [[ definition above, but without globbing and without =~ .
779
780 for id_name, token_str in [('Equal', '='), ('DEqual', '=='),
781 ('NEqual', '!=')]:
782 id_int = id_spec.AddBoolBinaryForBuiltin(id_name, Kind['BoolBinary'])
783
784 binary_lookup[token_str] = id_int
785
786 # Some of these names don't quite match, but it keeps the BoolParser simple.
787 binary_lookup['<'] = Id['Op_Less']
788 binary_lookup['>'] = Id['Op_Great']
789
790 # NOTE: -a and -o overloaded as unary prefix operators BoolUnary_a and
791 # BoolUnary_o. The parser rather than the tokenizer handles this.
792 other_lookup['!'] = Id['KW_Bang'] # like [[ !
793 other_lookup['('] = Id['Op_LParen']
794 other_lookup[')'] = Id['Op_RParen']
795
796 other_lookup[']'] = Id['Arith_RBracket'] # For closing ]