Skip to content

Commit

Permalink
v1.3.1: Some frontend bug fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
rindPHI committed Sep 30, 2022
1 parent a429f57 commit c1a4ae3
Show file tree
Hide file tree
Showing 6 changed files with 50 additions and 24 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,12 @@ This file contains the notable changes in the ISLa project since version 0.2a1

## [unreleased]

## [1.3.1] - 2022-09-30

### Changed

- Fixed bug in `unparse_grammar`: Backslashes are correctly escaped now.
- Fixed bug in parsing of match expressions: Escaped symbols are not handled correctly.
- Made order of terminals in REST_GRAMMAR deterministic.

## [1.3.0] - 2022-09-27
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ build-backend = "setuptools.build_meta"

[project]
name = "isla-solver"
version = "1.3.0"
version = "1.3.1"
authors = [
{ name = "Dominic Steinhöfel", email = "dominic.steinhoefel@cispa.de" },
]
Expand Down
2 changes: 1 addition & 1 deletion src/isla/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
__version__ = "1.3.0"
__version__ = "1.3.1"
19 changes: 19 additions & 0 deletions src/isla/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -798,3 +798,22 @@ def result(inp: T) -> T:
return inp

return result


def instantiate_escaped_symbols(text: str) -> str:
backslash_escape_placeholder = "$$BESC$$"
assert backslash_escape_placeholder not in text

repl_map = {
"\\b": "\b",
"\\t": "\t",
"\\n": "\n",
"\\r": "\r",
'\\"': '"',
}

text = text.replace("\\\\", backslash_escape_placeholder)
for escaped_char in repl_map:
text = text.replace(escaped_char, repl_map[escaped_char])

return text.replace(backslash_escape_placeholder, "\\")
24 changes: 4 additions & 20 deletions src/isla/language.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@
chain_functions,
eassert,
Exceptional,
instantiate_escaped_symbols,
)
from isla.helpers import (
replace_line_breaks,
Expand Down Expand Up @@ -2681,10 +2682,8 @@ def exitMatchExprOptional(self, ctx: MexprParser.MatchExprOptionalContext):

def exitMatchExprChars(self, ctx: MexprParser.MatchExprCharsContext):
text = antlr_get_text_with_whitespace(ctx)
text = text.replace("{{", "{")
text = text.replace("}}", "}")
text = text.replace('\\"', '"')
self.result.append(text)
text = text.replace("{{", "{").replace("}}", "}")
self.result.append(instantiate_escaped_symbols(text))

def exitMatchExprVar(self, ctx: MexprParser.MatchExprVarContext):
self.result.append(
Expand Down Expand Up @@ -3809,22 +3808,7 @@ def exitAlternative(self, ctx: bnfParser.AlternativeContext):
assert child_text[-1] == '"'
child_text = child_text[1:-1]

# Instantiate escaped characters
backslash_escape_placeholder = "$$BESC$$"
assert backslash_escape_placeholder not in child_text
child_text = child_text.replace("\\\\", backslash_escape_placeholder)
repl_map = {
"\\b": "\b",
"\\t": "\t",
"\\n": "\n",
"\\r": "\r",
'\\"': '"',
}
for escaped_char in repl_map:
child_text = child_text.replace(escaped_char, repl_map[escaped_char])
child_text = child_text.replace(backslash_escape_placeholder, "\\")

elems.append(child_text)
elems.append(instantiate_escaped_symbols(child_text))
self.partial_results[ctx] = "".join(elems)


Expand Down
24 changes: 22 additions & 2 deletions tests/test_concrete_syntax.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
unparse_isla,
parse_bnf,
unparse_grammar,
ISLaEmitter,
)
from isla.z3_helpers import z3_eq
from isla_formalizations import scriptsizec
Expand Down Expand Up @@ -601,8 +602,8 @@ def test_unparse_grammar(self):
def test_parse_bnf_with_escape_chars(self):
grammar_str = rf'''
<start> ::= <A>
<A> ::= "\r" | "\n" | "\"" | "\\t" | "\\\\\\"'''
expected = {"<start>": ["<A>"], "<A>": ["\r", "\n", '"', "\\t", "\\\\\\"]}
<A> ::= "\\a\\" | "\r" | "\n" | "\"" | "\\t" | "\\\\\\"'''
expected = {"<start>": ["<A>"], "<A>": ["\\a\\", "\r", "\n", '"', "\\t", "\\\\\\"]}

self.assertEqual(expected, parse_bnf(grammar_str))

Expand Down Expand Up @@ -773,8 +774,27 @@ def test_unbound_variable(self):
def test_parse_rest_bnf(self):
unparsed = unparse_grammar(REST_GRAMMAR)
parsed = parse_bnf(unparsed)
print(parsed)
gg.GrammarGraph.from_grammar(parsed)

def test_parse_match_expression_with_escape_char(self):
isla_emitter = ISLaEmitter(None)

DummyVariable.cnt = 0
parsed = isla_emitter.parse_mexpr(
r"{<a> a}a\\n\n\r{<b> b}<c>", VariableManager()
)

DummyVariable.cnt = 0
expected = language.BindExpression(
language.BoundVariable("a", "<a>"),
DummyVariable("a\\n\n\r"),
language.BoundVariable("b", "<b>"),
language.DummyVariable("<c>"),
)

self.assertEqual(expected, parsed)


if __name__ == "__main__":
unittest.main()

0 comments on commit c1a4ae3

Please sign in to comment.