Skip to content

Commit 7738dfb

Browse files
committed
Fix exception in the ParserRuleContext.to_position extension method when the input stream is empty, release version 0.7.1
1 parent ef6cd2a commit 7738dfb

6 files changed

Lines changed: 45 additions & 5 deletions

File tree

CHANGELOG.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,19 @@
22
All notable changes to this project from version 0.4.0 upwards are documented in this file.
33
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).
44

5+
## [0.7.1] – 2024-05-16
6+
7+
### Fixed
8+
- `ParserRuleContext.to_position` extension method when the input stream is empty
9+
10+
## [0.7.0] – 2023-11-21
11+
12+
### Added
13+
- `Point.isBefore` method as in Kolasu
14+
15+
### Fixed
16+
- Bug in the deserialization of Result
17+
518
## [0.6.0] – 2023-10-10
619

720
### Added

README.md

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,18 +18,31 @@ python -m unittest discover tests
1818
flake8 . && flake8 tests
1919
```
2020

21+
## Testing
22+
23+
```shell
24+
pytest tests
25+
```
26+
2127
## Packaging and Distribution
2228

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

26-
Ensure that you have build and twine installed:
32+
Let's ensure that we have build and twine installed:
2733

2834
```shell
2935
pip install build twine
3036
```
3137

32-
Then run:
38+
Then, check the project can be released by linting and running the test suite:
39+
40+
```shell
41+
flake8 . && flake8 tests
42+
pytest tests
43+
```
44+
45+
Finally, we can run:
3346

3447
```shell
3548
rm dist/*

pylasu/__version__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
__version__ = "0.7.0"
1+
__version__ = "0.7.1"

pylasu/parsing/parse_tree.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,8 @@ def token_end_point(token: Token):
4343

4444
@extension_method(ParserRuleContext)
4545
def to_position(self: ParserRuleContext, source: Source = None):
46-
if self.start.start_point <= self.stop.end_point:
46+
# In case of an empty input, the start token will be EOF and the end token will be None
47+
if self.stop and self.start.start_point <= self.stop.end_point:
4748
return Position(self.start.start_point, self.stop.end_point, source)
4849
else:
4950
# In case of parse errors, sometimes ANTLR inserts nodes that end before they start

setup.cfg

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[metadata]
22
name = Pylasu
3-
version: 0.7.0
3+
version: 0.7.1
44

55
[coverage:run]
66
branch = True

tests/test_parse_tree.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44

55
from pylasu.model import Point
66
from pylasu.parsing.parse_tree import ParseTreeOrigin, generate_nodes_classes_for_parser
7+
from tests.antlr_script.AntlrScriptLexer import AntlrScriptLexer
8+
from tests.antlr_script.AntlrScriptParser import AntlrScriptParser
79
from tests.simple_lang.SimpleLangLexer import SimpleLangLexer
810
from tests.simple_lang.SimpleLangParser import SimpleLangParser
911

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

24+
def test_empty_parse_tree_position(self):
25+
lexer = AntlrScriptLexer(InputStream(""))
26+
parser = AntlrScriptParser(CommonTokenStream(lexer))
27+
parse_tree = parser.script()
28+
self.assertIsNone(parse_tree.stop)
29+
origin = ParseTreeOrigin(parse_tree)
30+
position = origin.position
31+
self.assertIsNotNone(position)
32+
self.assertEqual(position.start, Point(1, 0))
33+
self.assertEqual(position.end, Point(1, 0))
34+
2235
def test_ast_gen(self):
2336
generate_nodes_classes_for_parser(SimpleLangParser, globals())
2437
self.assertTrue("CompilationUnit" in globals())

0 commit comments

Comments
 (0)