def testEnum(self): Color = util.Enum('Color', 'red green blue'.split()) print(Color._values) print(Color._lookup) Color = util.Enum('Color', ['red', ('green', 3), 'blue']) print(Color._values) print(Color._lookup) print(Color.red) print(Color.green) try: print(Color.BAD) except AttributeError as e: self.assertEqual('BAD', e.args[0]) else: self.fail("Expected error") self.assertEqual(Color.red, Color.red) self.assertNotEqual(Color.red, Color.green) self.assertEqual(Color.red, 0) self.assertEqual(Color.blue, 4) try: print(Color.blue == '') except ValueError as e: pass else: self.fail("Expected error")
'Function', ]) # Assignment builtins -- treated as statically parsed keywords. They are # different from keywords because env bindings can appear before, e.g. # FOO=bar local v. # "None" could either be a global variable or assignment to a local. spec.AddKind('Assign', ['Declare', 'Export', 'Local', 'Readonly', 'None']) # Id -> OperandType BOOL_OPS = {} # type: dict UNARY_FILE_CHARS = tuple('abcdefghLprsStuwxOGN') OperandType = util.Enum('OperandType', 'Undefined Path Int Str Other'.split()) def _Dash(strs): # Gives a pair of (token name, string to match) return [(s, '-' + s) for s in strs] def _AddBoolKinds(spec): spec.AddBoolKind( 'BoolUnary', { OperandType.Str: _Dash(list('zn')), # -z -n OperandType.Other: _Dash(list('ovR')), OperandType.Path: _Dash(UNARY_FILE_CHARS), })
""" if node.tag == command_e.SimpleCommand: return node assert hasattr(node, 'children'), node n = len(node.children) if n == 0: return None # Go as deep as we need. return _FindLastSimpleCommand(node.children[-1]) ECompletionType = util.Enum( 'ECompletionState', 'NONE FIRST REST VAR_NAME HASH_KEY REDIR_FILENAME'.split()) # REDIR_FILENAME: stdin only # HASH_KEY: do this later # NONE: nothing detected, should we default to filenames/directories? # Note: this could also be a CompRequest # CompRequest(FIRST, prefix) # CompRequest(REST, prefix, comp_words) # use the full contact # CompRequest(VAR_NAME, prefix) # CompRequest(HASH_KEY, prefix, hash_name) # use var name # CompRequest(REDIR_FILENAME, prefix) # Or it could just be a Completer / Chain? # CommandCompleter
the path search in step 1d. """ import sys from core import util # NOTE: NONE is a special value. # TODO: # - Make a table of name to enum? source, dot, etc. # - So you can just add "complete" and have it work. EBuiltin = util.Enum('EBuiltin', """ NONE READ ECHO CD PUSHD POPD EXPORT EXIT SOURCE DOT TRAP EVAL EXEC SET COMPLETE COMPGEN DEBUG_LINE """.split()) # These can't be redefined by functions. # http://pubs.opengroup.org/onlinepubs/9699919799/utilities/V3_chap02.html#tag_18_14 # On the other hand, 'cd' CAN be redefined. # TODO: # - use these SPECIAL_BUILTINS = [ 'break', ':', 'continue', '.', 'eval', 'exec', 'exit', 'export', 'readonly', 'return', 'set', 'shift', 'times', 'trap', 'unset', # local and declare are not POSIX, but should be here since export and # readonly are.
for p in part_vals: if p.tag == part_value_e.StringPartValue: out.append(p.s) else: last = len(p.strs) - 1 for i, s in enumerate(p.strs): out.append(s) if i != last: out.append(join_char) return ''.join(out) # SliceParts is for ${a-} and ${a+}, Error is for ${a?}, and SliceAndAssign is # for ${a=}. Effect = util.Enum('Effect', 'SpliceParts Error SpliceAndAssign NoOp'.split()) class _WordEvaluator: """Abstract base class for word evaluators. Public entry points: EvalWordToString EvalRhsWord EvalWordSequence """ def __init__(self, mem, exec_opts, splitter): self.mem = mem # for $HOME, $1, etc. self.exec_opts = exec_opts # for nounset self.splitter = splitter self.globber = glob_.Globber(exec_opts)
from core.id_kind import Id, Kind, ID_SPEC from core import util from core.lexer import C, R import re # Thirteen lexer modes for osh. # Possible additional modes: # - extended glob? # - nested backticks: echo `echo \`echo foo\` bar` LexMode = util.Enum( 'LexMode', """ NONE COMMENT OUTER DBRACKET SQ DQ DOLLAR_SQ ARITH VS_1 VS_2 VS_ARG_UNQ VS_ARG_DQ BASH_REGEX BASH_REGEX_CHARS """.split()) # In oil, I hope to have these lexer modes: # COMMAND # EXPRESSION (takes place of ARITH, VS_UNQ_ARG, VS_DQ_ARG) # SQ RAW_SQ DQ RAW_DQ # VS -- a single state here? Or switches into expression state, because } # is an operator # Problem: DICT_KEY might be a different state, to accept either a bare word # foo, or an expression (X=a+2), which is allowed in shell. Python doesn't # allowed unquoted words, but we want to.
self.body_str = body_str def Run(self): """ do_exit: For small pipelines """ #log('Writing %r', self.body_str) os.write(self.w, self.body_str) #log('Wrote %r', self.body_str) os.close(self.w) #log('Closed %d', self.w) sys.exit(0) # Could this fail? ProcessState = util.Enum('ProcessState', """Init Done""".split()) class Job(object): def __init__(self): self.state = ProcessState.Init def State(self): return self.state def WaitUntilDone(self, waiter): """ Returns: An int for a process A list of ints for a pipeline """
# const foo = $HOME/hello # const foo = $~/bar # hm I kind of don't like this but OK # const foo = "$~/bar" # const foo = [ ~/bar ][0] # does this make sense? # const foo = `~/bar` # I think ~ should be like $ -- special. Maybe even inside double quotes? # Or only at the front? SPLIT, EXPR, UNQUOTED, DQ, SQ = range(5) # 5 modes of expression # DQ: \$ \\ \" # SQ: \\ \' WordStyle = util.Enum('WordStyle', 'Expr Unquoted DQ SQ'.split()) # QEFS is wrong? Because RHS never gets split! It can always be foo=$1/foo. # Not used because RHS not split: # $x -> @-x and ${x} -> @-x # ${x:-default} -> @-(x or 'default') def _GetRhsStyle(w): # NOTE: Pattern matching style would be a lot nicer for this... # Arith and command sub both retain $() and $[], so they are not pure # "expressions". VAR_SUBS = (word_part_e.SimpleVarSub, word_part_e.BracedVarSub, word_part_e.TildeSubPart) OTHER_SUBS = (word_part_e.CommandSubPart, word_part_e.ArithSubPart)
scope = runtime.scope var_flags = runtime.var_flags log = util.log # NOTE: NONE is a special value. # TODO: # - Make a table of name to enum? source, dot, etc. # - So you can just add "complete" and have it work. EBuiltin = util.Enum( 'EBuiltin', """ NONE READ ECHO SHIFT CD PUSHD POPD DIRS EXPORT UNSET SET SHOPT TRAP UMASK EXIT SOURCE DOT EVAL EXEC WAIT JOBS COMPLETE COMPGEN DEBUG_LINE TRUE FALSE COLON TEST BRACKET TYPE HELP """.split()) # These can't be redefined by functions. # http://pubs.opengroup.org/onlinepubs/9699919799/utilities/V3_chap02.html#tag_18_14 # On the other hand, 'cd' CAN be redefined. # # NOTE: OSH treats these specially: # - break/continue/return # - local/readonly _SPECIAL_BUILTINS = [
shall not be recognized at this point. It shall be invoked in conjunction with the path search in step 1d. """ import sys from core import util # NOTE: NONE is a special value. # TODO: # - Make a table of name to enum? source, dot, etc. # - So you can just add "complete" and have it work. EBuiltin = util.Enum('EBuiltin', """ NONE BREAK CONTINUE RETURN READ ECHO EXIT SOURCE DOT TRAP EVAL EXEC SET COMPLETE COMPGEN DEBUG_LINE """.split()) # These can't be redefined by functions. # http://pubs.opengroup.org/onlinepubs/9699919799/utilities/V3_chap02.html#tag_18_14 # On the other hand, 'cd' CAN be redefined. # TODO: # - use these # - local and declare should be here, since export and readonly are. SPECIAL_BUILTINS = [ 'break', ':', 'continue', '.', 'eval', 'exec', 'exit', 'export', 'readonly', 'return', 'set', 'shift', 'times', 'trap', 'unset', ]