-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
19 changed files
with
657 additions
and
79 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
lines: _EOL* line* [_SOL] | ||
|
||
?line: [_SOL] option _EOL+ | ||
|
||
?option: prepend | ||
| prepend_path | ||
| append | ||
| append_path | ||
| remove | ||
| clear | ||
| assign | ||
|
||
!?append: keys OP_APPEND value -> full | ||
!?append_path: keys OP_APPEND_PATH value -> full | ||
!?remove: keys OP_REMOVE value -> full | ||
!?prepend: keys OP_PREPEND value -> full | ||
!?prepend_path: keys OP_PREPEND_PATH value -> full | ||
!?clear: keys OP_CLEAR -> full | ||
!?assign: keys OP_ASSIGN value -> full | ||
|
||
OP_APPEND.2: "+=" | ||
OP_APPEND_PATH.3: "+=(path)" | ||
OP_PREPEND.2: "=+" | ||
OP_PREPEND_PATH.3: "=+(path)" | ||
OP_REMOVE.2: "-=" | ||
OP_CLEAR.2: "=!" | ||
OP_ASSIGN.1: "=" | ||
|
||
keys: key ("." key)* [_WS] | ||
|
||
?value : object | ||
| array | ||
| NUMBER | ||
| TRUE | ||
| FALSE | ||
| string | ||
|
||
?sub_value : object | ||
| array | ||
| NUMBER | ||
| TRUE | ||
| FALSE | ||
| string_no_ws | ||
|
||
object : "{" [pair ("," pair)*] "}" | ||
array : "[" ( [_WS] sub_value [_WS] ("," [_WS] sub_value [_WS])* ["," [_WS]] )? "]" | ||
NUMBER.2 : /-?[0-9]+/ | ||
TRUE.2 : "true" | "True" | ||
FALSE.2 : "false" | "False" | ||
pair : [_WS] dict_key [_WS] "=" [_WS] sub_value [_WS] | ||
|
||
key : ESCAPED_STRING -> escaped_string | ||
| UNQUOTED_KEY -> unquoted_string | ||
string : ESCAPED_STRING -> escaped_string | ||
| UNQUOTED_STRING -> unquoted_string | ||
string_no_ws : ESCAPED_STRING -> escaped_string | ||
| UNQUOTED_STRING_NO_WS -> unquoted_string | ||
dict_key : ESCAPED_STRING -> escaped_string | ||
| UNQUOTED_DICT_KEY -> unquoted_string | ||
|
||
_WS: /[ \t\f\r\n]+/ | ||
_SOL: /[ \t]+/ | ||
_EOL: /[ \t\f\r]*\n/ | ||
UNQUOTED_KEY: /[a-zA-Z0-9_-]*[a-zA-Z0-9_]/ | ||
UNQUOTED_STRING: /[^"\[\]{}\r\n#]+/ | ||
UNQUOTED_STRING_NO_WS: /[^"\[\]{},\r\n# \t\f]+/ | ||
UNQUOTED_DICT_KEY: /[^"\[\]{},\r\n# \t\f=]+/ | ||
|
||
%import common.ESCAPED_STRING | ||
%import common.SH_COMMENT | ||
%ignore [_WS] SH_COMMENT |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
from __future__ import annotations | ||
|
||
from dataclasses import dataclass | ||
from pathlib import Path | ||
from typing import cast | ||
|
||
from lark import Lark, Token, Transformer, v_args | ||
|
||
grammar = Path(__file__).with_suffix(".lark").read_text() | ||
|
||
|
||
@dataclass | ||
class CLIOption: | ||
action: str | ||
key: tuple[str, ...] | ||
value: bool | str | list | dict | float | None | ||
|
||
|
||
class TreeToCLIOption(Transformer): | ||
@v_args(inline=True) | ||
def escaped_string(self, s: Token): | ||
return s[1:-1].encode("raw_unicode_escape").decode("unicode_escape") | ||
|
||
@v_args(inline=True) | ||
def unquoted_string(self, s: Token): | ||
return s.value | ||
|
||
@v_args(inline=True) | ||
def full(self, k, a, v=None): | ||
return CLIOption(a.value, k, v) | ||
|
||
lines = list | ||
keys = tuple | ||
|
||
array = list | ||
pair = tuple | ||
object = dict | ||
|
||
NUMBER = int | ||
TRUE = lambda self, _: True | ||
FALSE = lambda self, _: False | ||
|
||
|
||
cli_parser = Lark(grammar, start="option", parser="lalr", transformer=TreeToCLIOption()) | ||
file_parser = Lark(grammar, start="lines", parser="lalr", transformer=TreeToCLIOption()) | ||
|
||
|
||
def parse_cli(s: str) -> CLIOption: | ||
return cast(CLIOption, cli_parser.parse(s)) | ||
|
||
|
||
def parse_file(s: str) -> list[CLIOption]: | ||
return cast(list[CLIOption], file_parser.parse(s)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.