Skip to content

Commit

Permalink
Fix tests; use previous symbol for missing ;
Browse files Browse the repository at this point in the history
  • Loading branch information
WeixuanZ committed Jun 2, 2022
1 parent 6eff079 commit 460826b
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 21 deletions.
12 changes: 9 additions & 3 deletions src/parse.py
Original file line number Diff line number Diff line change
Expand Up @@ -268,7 +268,9 @@ def parse_devices_statement(self):
return None

if not self.current_symbol.type == OperatorType.SEMICOLON:
self.throw_error(SyntaxErrors.UnexpectedToken, "Expected ';'")
self.throw_error(
SyntaxErrors.UnexpectedToken, "Expected ';'", prev_word=True
)
return False

if self.syntax_valid:
Expand Down Expand Up @@ -316,7 +318,9 @@ def parse_device_type(self):

if not self.current_symbol.type == OperatorType.LEFT_ANGLE:
self.throw_error(
SyntaxErrors.UnexpectedToken, "Expected '<' or ';'"
SyntaxErrors.UnexpectedToken,
"Expected '<' or ';'",
prev_word=True,
)
return False, None

Expand Down Expand Up @@ -455,7 +459,9 @@ def parse_connection_statement(self):
return None

if not self.current_symbol.type == OperatorType.SEMICOLON:
self.throw_error(SyntaxErrors.UnexpectedToken, "Expected ';'")
self.throw_error(
SyntaxErrors.UnexpectedToken, "Expected ';'", prev_word=True
)
return False

if self.syntax_valid:
Expand Down
14 changes: 8 additions & 6 deletions src/scanner.py
Original file line number Diff line number Diff line change
Expand Up @@ -106,9 +106,12 @@ class Scanner:
pointer_colno:
Column number of the pointer.
LINE_COMMENT_IDENTIFIER:
Identifier specifying start of line comments.
Identifier specifying start of line comments, default '//'.
BLOCK_COMMENT_IDENTIFIERS:
Identifiers specifying start and end of block comments.
Identifiers specifying start and end of block comments,
default ('/*', '*/').
TREAT_INVALID_CHAR_AS_ERROR:
Whether to throw errors for invalid characters, default True.
EOF:
Symbol indicating the end of file.
Expand Down Expand Up @@ -578,18 +581,17 @@ def get_symbol(self) -> Union[Symbol, None]:
if Scanner.TREAT_INVALID_CHAR_AS_ERROR:
error = SyntaxErrors.UnexpectedToken("Invalid character")
symbol_lineno, symbol_colno = self.get_lineno_colno(
self._pointer_pos - 1
self._pointer_pos
)
[symbol_id] = self.names.lookup([current_character])
error.symbol = Symbol(
symbol_type=ExternalSymbolType.IDENTIFIER,
symbol_id=symbol_id,
symbol_id=-1,
lineno=symbol_lineno,
colno=symbol_colno,
)
self.errors.add_error(
error=error,
show_end_of_word=True,
show_end_of_word=False,
parse_entry_func_name="get_symbol",
base_depth=0,
)
Expand Down
13 changes: 5 additions & 8 deletions tests/test_integration.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
from network import Network
from names import Names
from monitors import Monitors
from exceptions import Errors


@pytest.fixture()
Expand Down Expand Up @@ -40,18 +41,14 @@ def names():


@pytest.fixture()
def scanner(input_file, names):
"""Create a scanner instance with the test file."""
return Scanner(input_file, names)


@pytest.fixture()
def parser(names, scanner):
def parser(names, input_file):
"""Create a parser instance."""
devices = Devices(names)
network = Network(names, devices)
monitors = Monitors(names, devices, network)
return Parser(names, devices, network, monitors, scanner)
errors = Errors()
scanner = Scanner(input_file, names, errors)
return Parser(names, devices, network, monitors, scanner, errors)


def test_parse_definition_file(parser):
Expand Down
5 changes: 3 additions & 2 deletions tests/test_parse.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from scanner import Symbol
from names import Names
from symbol_types import OperatorType, DeviceType
from exceptions import SyntaxErrors, SemanticErrors
from exceptions import SyntaxErrors, SemanticErrors, Errors


class MockScanner:
Expand Down Expand Up @@ -37,8 +37,9 @@ def make_parser(statement):
names.lookup(statement)
devices = Devices(names)
scanner = MockScanner(names, statement)
errors = Errors()

return Parser(names, devices, None, None, scanner)
return Parser(names, devices, None, None, scanner, errors)


@pytest.mark.parametrize(
Expand Down
16 changes: 14 additions & 2 deletions tests/test_scanner.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,18 @@
)


class StubErrors:
def add_error(
self,
error,
show_end_of_word,
show_cursor=True,
parse_entry_func_name="parse_network",
base_depth=2,
):
return


def test_symbol_equal():
symbol1 = Symbol(
symbol_type=ExternalSymbolType.IDENTIFIER,
Expand Down Expand Up @@ -62,7 +74,7 @@ def input_file(tmp_path, file_content):
@pytest.fixture()
def scanner(input_file):
"""Create a scanner instance with the test file."""
return Scanner(input_file, Names())
return Scanner(input_file, Names(), StubErrors()) # noqa


# -----------------------------------------------------------------------------
Expand Down Expand Up @@ -582,7 +594,7 @@ def test_get_symbol(tmp_path, monkeypatch, content, expected_symbols):

monkeypatch.setattr("names.ReservedSymbolType", ReservedSymbolType)
monkeypatch.setattr("scanner.ReservedSymbolType", ReservedSymbolType)
scanner = Scanner(p, Names())
scanner = Scanner(p, Names(), StubErrors()) # noqa

expected_symbols = iter(expected_symbols)
while (symbol := scanner.get_symbol()) is not None:
Expand Down

0 comments on commit 460826b

Please sign in to comment.