shell.grammer package

Submodules

shell.grammer.literals module

shell.grammer.parse_functions module

shell.grammer.parse_functions.dataclass_to_string(data)[source]

Convert a dataclass to a string.

shell.grammer.parse_functions.dict_to_string(d: dict) str[source]
shell.grammer.parse_functions.expand_variables(terms: List[str], env: dict, completenames_options: List[str], ignore_invalid_syntax: bool, wildcard_value='$', escape=False) Iterable[str][source]

Replace variables with their values. E.g.

a = 1
print $a # gets converted to `print 1`
shell.grammer.parse_functions.expand_variables_inline(line: str, env: dict, completenames_options: List[str], ignore_invalid_syntax: bool) str[source]

Expand $variables in line.

shell.grammer.parse_functions.filter_comments(terms: List[str]) List[str][source]
shell.grammer.parse_functions.indent_width(line: str) Tuple[str, str][source]

Return a tuple that represents the length of the indentation in spaces and tabs.

shell.grammer.parse_functions.inline_indent_with(*indent_per_type) float[source]
shell.grammer.parse_functions.list_to_string(items: list) str[source]
shell.grammer.parse_functions.quote_items(items: List[str]) Iterable[str][source]

Map shlex.quote() to all items. Do not modify python delimiters.

shell.grammer.parse_functions.quote_return_value(value: str, delimiter='\n') str[source]
shell.grammer.parse_functions.to_bool(line: str) bool[source]
shell.grammer.parse_functions.to_string(value: Any) str[source]

Convert a variable to a string.

shell.grammer.parser module

Parse tokens. Tokens are defined in shell.grammer.tokenizer

Parsing rules;

lines: a BREAK-separated sequence of line
line: statement with optional INDENT
statement: 
    - assignment        # a = 1
    - conditional       # if .. then ..
    - conjunction       # add 1 |> echo
    - definition        # f (x): ..
    - return_statement  # return 1
conjunction: a PIPE-separated sequence of expression
expression: a command

Lines with an assignment.

# a b   = 10 11  ;      print $a   ;
|-------------||-----||----------||-----|
 assignment     break  expression  break
|-----| |-----|
 terms   terms

A line with a conjunction.

# range 1 5  |>    print '# ' |>    print
|----------------------------------------------| (conjunction)
|----------||----||----------------------------|
 expression  pipe  conjunction
|----------|      |----------||----||----------|
 terms             expression  pipe  expression
                  |----------|      |----------|
                  terms              terms
shell.grammer.parser.parse(text, init=True)[source]

Implement ply methods

shell.grammer.parsetab module

shell.grammer.tokenizer module

shell.grammer.tokenizer.main()[source]

Token regexes are defined with the prefix t_. From ply docs:

  • functions are matched in order of specification

  • strings are sorted by regular expression length

shell.grammer.tokenizer.tokenize(data: str)[source]

Module contents