Skip to content

Commit

Permalink
Fix exception in the ParserRuleContext.to_position extension method…
Browse files Browse the repository at this point in the history
… when the input stream is empty, release version 0.7.1
  • Loading branch information
alessiostalla committed May 16, 2024
1 parent ef6cd2a commit 7738dfb
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 5 deletions.
13 changes: 13 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,19 @@
All notable changes to this project from version 0.4.0 upwards are documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).

## [0.7.1] – 2024-05-16

### Fixed
- `ParserRuleContext.to_position` extension method when the input stream is empty

## [0.7.0] – 2023-11-21

### Added
- `Point.isBefore` method as in Kolasu

### Fixed
- Bug in the deserialization of Result

## [0.6.0] – 2023-10-10

### Added
Expand Down
17 changes: 15 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,18 +18,31 @@ python -m unittest discover tests
flake8 . && flake8 tests
```

## Testing

```shell
pytest tests
```

## Packaging and Distribution

Update version in `setup.cfg` and `pylasu/__version__.py` _(TODO do we need both?)_,
commit and check that CI completes normally.

Ensure that you have build and twine installed:
Let's ensure that we have build and twine installed:

```shell
pip install build twine
```

Then run:
Then, check the project can be released by linting and running the test suite:

```shell
flake8 . && flake8 tests
pytest tests
```

Finally, we can run:

```shell
rm dist/*
Expand Down
2 changes: 1 addition & 1 deletion pylasu/__version__.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
__version__ = "0.7.0"
__version__ = "0.7.1"
3 changes: 2 additions & 1 deletion pylasu/parsing/parse_tree.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,8 @@ def token_end_point(token: Token):

@extension_method(ParserRuleContext)
def to_position(self: ParserRuleContext, source: Source = None):
if self.start.start_point <= self.stop.end_point:
# In case of an empty input, the start token will be EOF and the end token will be None
if self.stop and self.start.start_point <= self.stop.end_point:
return Position(self.start.start_point, self.stop.end_point, source)
else:
# In case of parse errors, sometimes ANTLR inserts nodes that end before they start
Expand Down
2 changes: 1 addition & 1 deletion setup.cfg
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[metadata]
name = Pylasu
version: 0.7.0
version: 0.7.1

[coverage:run]
branch = True
Expand Down
13 changes: 13 additions & 0 deletions tests/test_parse_tree.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

from pylasu.model import Point
from pylasu.parsing.parse_tree import ParseTreeOrigin, generate_nodes_classes_for_parser
from tests.antlr_script.AntlrScriptLexer import AntlrScriptLexer
from tests.antlr_script.AntlrScriptParser import AntlrScriptParser
from tests.simple_lang.SimpleLangLexer import SimpleLangLexer
from tests.simple_lang.SimpleLangParser import SimpleLangParser

Expand All @@ -19,6 +21,17 @@ def test_parse_tree_origin(self):
self.assertEqual(position.start, Point(1, 0))
self.assertEqual(position.end, Point(2, 2))

def test_empty_parse_tree_position(self):
lexer = AntlrScriptLexer(InputStream(""))
parser = AntlrScriptParser(CommonTokenStream(lexer))
parse_tree = parser.script()
self.assertIsNone(parse_tree.stop)
origin = ParseTreeOrigin(parse_tree)
position = origin.position
self.assertIsNotNone(position)
self.assertEqual(position.start, Point(1, 0))
self.assertEqual(position.end, Point(1, 0))

def test_ast_gen(self):
generate_nodes_classes_for_parser(SimpleLangParser, globals())
self.assertTrue("CompilationUnit" in globals())
Expand Down

0 comments on commit 7738dfb

Please sign in to comment.