Skip to content

Commit

Permalink
format
Browse files Browse the repository at this point in the history
  • Loading branch information
dpranke committed Nov 18, 2024
1 parent ff8f96b commit 76110df
Show file tree
Hide file tree
Showing 6 changed files with 57 additions and 30 deletions.
10 changes: 7 additions & 3 deletions src/pyfloyd/analyzer.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,9 @@ def __init__(self):
self.choices = {}


def analyze(ast, rewrite_filler: bool, rewrite_subrules: bool, global_vars=None) -> Grammar:
def analyze(
ast, rewrite_filler: bool, rewrite_subrules: bool, global_vars=None
) -> Grammar:
"""Analyze and optimize the AST.
This runs any static analysis we can do over the grammars and
Expand Down Expand Up @@ -496,15 +498,17 @@ def _rewrite_filler(grammar):
grammar.ast[2] = [
rule
for rule in grammar.ast[2]
if rule[1] not in ('%whitespace', '%comment', '%globals', '%token', '%tokens')
if rule[1]
not in ('%whitespace', '%comment', '%globals', '%token', '%tokens')
]
grammar.comment = None
grammar.whitespace = None
grammar.tokens = set()
grammar.pragmas = [
n
for n in grammar.pragmas
if n[1] not in ('%whitespace', '%comment', '%globals', '%token', '%tokens')
if n[1]
not in ('%whitespace', '%comment', '%globals', '%token', '%tokens')
]


Expand Down
20 changes: 14 additions & 6 deletions src/pyfloyd/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,12 @@ class ParserInterface(Protocol):
`compile()`.
"""

def parse(self, text: str, path: str = '<string>', global_vars: Optional[Dict[str, Any]] = None) -> Result:
def parse(
self,
text: str,
path: str = '<string>',
global_vars: Optional[Dict[str, Any]] = None,
) -> Result:
"""Parse a string and return a result.
`text` is the string to parse.
Expand Down Expand Up @@ -84,7 +89,7 @@ def generate(
grammar: str,
path: str = '<string>',
options: Optional[GeneratorOptions] = None,
global_vars = None
global_vars=None,
) -> Result:
"""Generate the source code of a parser.
Expand Down Expand Up @@ -122,7 +127,10 @@ def generate(
return result
try:
grammar_obj = analyzer.analyze(
result.val, rewrite_filler=True, rewrite_subrules=True, global_vars=global_vars
result.val,
rewrite_filler=True,
rewrite_subrules=True,
global_vars=global_vars,
)
except analyzer.AnalysisError as e:
return Result(err=str(e))
Expand All @@ -143,7 +151,7 @@ def parse(
text: str,
grammar_path: str = '<string>',
path: str = '<string>',
global_vars : Dict[str, Any] = None,
global_vars: Dict[str, Any] = None,
memoize: bool = False,
) -> Result:
"""Match an input text against the specified grammar.
Expand Down Expand Up @@ -174,7 +182,7 @@ def parse(
def pretty_print(
grammar: str,
path: str = '<string>',
global_vars = None,
global_vars=None,
rewrite_filler: bool = False,
rewrite_subrules: bool = False,
) -> Tuple[Optional[str], Optional[str]]:
Expand Down Expand Up @@ -217,7 +225,7 @@ def pretty_print(
def dump_ast(
grammar: str,
path: str = '<string>',
global_vars = None,
global_vars=None,
rewrite_filler: bool = False,
rewrite_subrules: bool = False,
) -> Tuple[Optional[str], Optional[str]]:
Expand Down
4 changes: 3 additions & 1 deletion src/pyfloyd/interpreter.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,9 @@ def __init__(self, grammar, memoize):
self._regexps = {}
self._global_vars = {}

def parse(self, text: str, path: str = '<string>', global_vars = None) -> parser.Result:
def parse(
self, text: str, path: str = '<string>', global_vars=None
) -> parser.Result:
self._text = text
self._path = path
self._failed = False
Expand Down
2 changes: 1 addition & 1 deletion src/pyfloyd/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ class Result(NamedTuple):
pos: Optional[int] = None


def parse(text: str, path: str = '<string>', global_vars = None) -> Result:
def parse(text: str, path: str = '<string>', global_vars=None) -> Result:
"""Parse a given text and return the result.
If the parse was successful, `result.val` will be the returned value
Expand Down
18 changes: 11 additions & 7 deletions src/pyfloyd/tool.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,13 +78,12 @@ def main(argv=None, host=None):
language=args.language, main=args.main, memoize=args.memoize
)
contents, err, _ = pyfloyd.generate(
grammar,
path=args.grammar,
options=options
grammar, path=args.grammar, options=options
)
else:
contents, err, _ = _interpret_grammar(host, args, grammar,
global_vars)
contents, err, _ = _interpret_grammar(
host, args, grammar, global_vars
)

if err:
host.print(err, file=host.stderr)
Expand All @@ -110,8 +109,13 @@ def _parse_args(host, argv):
action='store_true',
help='compile grammar instead of interpreting it',
)
ap.add_argument('-D', '--define', action='append', default=[],
help='Define a global var=value')
ap.add_argument(
'-D',
'--define',
action='append',
default=[],
help='Define a global var=value',
)
ap.add_argument('-o', '--output', help='path to write output to')
ap.add_argument(
'-p',
Expand Down
33 changes: 21 additions & 12 deletions tests/grammar_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,9 @@ def check(
p.cleanup()

def checkp(self, parser, text, out=None, err=None, global_vars=None):
actual_out, actual_err, _ = parser.parse(text, path='<string>',
global_vars=global_vars)
actual_out, actual_err, _ = parser.parse(
text, path='<string>', global_vars=global_vars
)
# Test err before out because it's probably more helpful to display
# an unexpected error than it is to display an unexpected output.
self.assertMultiLineEqual(err or '', actual_err or '')
Expand Down Expand Up @@ -393,7 +394,9 @@ def test_json(self):
self.assertIsNone(err)
self._common_json_checks(p)

self.checkp(p, text='"foo"', out='"foo"', global_vars={'_strict': False})
self.checkp(
p, text='"foo"', out='"foo"', global_vars={'_strict': False}
)

if hasattr(p, 'cleanup'):
p.cleanup()
Expand All @@ -409,7 +412,7 @@ def test_json5(self):

@skip('integration')
def test_json5_special_floats(self):
gv = { '_strict': True }
gv = {'_strict': True}
h = pyfloyd.host.Host()
path = str(THIS_DIR / '../grammars/json5.g')
p, err, _ = self.compile(h.read_text_file(path))
Expand All @@ -429,7 +432,7 @@ def test_json5_special_floats(self):
p.cleanup()

def _common_json_checks(self, p):
gvs = { '_strict': True }
gvs = {'_strict': True}
self.checkp(p, text='123', out=123, global_vars=gvs)
self.checkp(p, text='1.5', out=1.5, global_vars=gvs)
self.checkp(p, text='-1.5', out=-1.5, global_vars=gvs)
Expand All @@ -443,21 +446,25 @@ def _common_json_checks(self, p):
self.checkp(p, text='{}', out={}, global_vars=gvs)

self.checkp(
p, text='[1', err='<string>:1 Unexpected end of input at column 3',
global_vars=gvs
p,
text='[1',
err='<string>:1 Unexpected end of input at column 3',
global_vars=gvs,
)

# Check that leading whitespace is allowed.
self.checkp(p, ' {}', {}, global_vars=gvs)

def _common_json5_checks(self, p):
gvs = { '_strict': False }
gvs = {'_strict': False}
self.checkp(p, text='+1.5', out=1.5, global_vars=gvs)
self.checkp(p, text='.5e-2', out=0.005, global_vars=gvs)
self.checkp(p, text='"foo"', out='foo', global_vars=gvs)
self.checkp(
p, text='{foo: "bar", a: "b"}', out={'foo': 'bar', 'a': 'b'},
global_vars=gvs
p,
text='{foo: "bar", a: "b"}',
out={'foo': 'bar', 'a': 'b'},
global_vars=gvs,
)

@skip('integration')
Expand Down Expand Up @@ -512,7 +519,7 @@ def test_json5_sample(self):
'trailing commas too',
],
},
global_vars={ '_strict': False }
global_vars={'_strict': False},
)
if hasattr(p, 'cleanup'):
p.cleanup()
Expand Down Expand Up @@ -1171,7 +1178,9 @@ def parse(self, text, path='<string>', global_vars=None):
for k, v in global_vars.items():
defines.extend(['-D', f'{k}={json.dumps(v)}'])
proc = subprocess.run(
['node', self.source] + defines + [inp], check=False, capture_output=True
['node', self.source] + defines + [inp],
check=False,
capture_output=True,
)
if proc.stderr:
stderr = proc.stderr.decode('utf8').strip()
Expand Down

0 comments on commit 76110df

Please sign in to comment.