Skip to content

Commit 9b0a75c

Browse files
added debug flag to lex and yacc
1 parent 97237bc commit 9b0a75c

2 files changed

Lines changed: 16 additions & 20 deletions

File tree

sifter/grammar/grammar.py

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6,46 +6,43 @@
66
from sifter.grammar.command import Command
77
from typing import (
88
Any,
9-
TYPE_CHECKING,
109
cast,
1110
Text
1211
)
1312

14-
import ply.yacc # type: ignore
13+
from ply.yacc import yacc, NullLogger, YaccError, LRParser, YaccProduction # type: ignore
1514

1615
from sifter.grammar.tag import Tag
1716
from sifter.grammar.command_list import CommandList
1817
from sifter.grammar.string import String
1918
from sifter.grammar.lexer import SieveLexer
2019
from sifter.extensions import ExtensionRegistry
2120

22-
if TYPE_CHECKING:
23-
from py.yacc import LRParser, YaccProduction # type: ignore
24-
2521

2622
class SieveParser():
2723

2824
tokens = SieveLexer.tokens
2925

30-
def __init__(self) -> None:
31-
self.lexer = SieveLexer()
26+
def __init__(self, debug: bool = False) -> None:
27+
self.lexer = SieveLexer(debug=debug)
3228
self.parser = self.make_parser(self)
3329
self.extensions = ExtensionRegistry()
3430

3531
@staticmethod
36-
def make_parser(mod: Any) -> 'LRParser':
37-
return ply.yacc.yacc(
32+
def make_parser(mod: Any, debug: bool = False) -> 'LRParser':
33+
return yacc(
3834
module=mod,
39-
debug=False,
40-
write_tables=False
35+
debug=debug,
36+
write_tables=False,
37+
errorlog=NullLogger() if not debug else None
4138
)
4239

4340
def parse(self, rules: Text, tracking: int = 0) -> CommandList:
4441
self.parser.errok()
4542

4643
rules = self.parser.parse(rules, self.lexer, tracking=tracking)
4744
if not self.parser.errorok:
48-
raise ply.yacc.YaccError('Syntax error')
45+
raise YaccError('Syntax error')
4946

5047
return cast(CommandList, rules)
5148

@@ -187,3 +184,6 @@ def p_stringlist_single(self, p: 'YaccProduction') -> None:
187184
def p_string(self, p: 'YaccProduction') -> None:
188185
"""string : QUOTED_STRING"""
189186
p[0] = String(p[1])
187+
188+
def p_error(self, p: 'YaccProduction') -> None:
189+
pass

sifter/grammar/lexer.py

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,25 +2,21 @@
22
# references are to sections in RFC 5228 unless stated otherwise.
33

44
from typing import (
5-
TYPE_CHECKING,
65
Any,
76
Optional,
87
Text
98
)
109

1110
import math
12-
import ply.lex # type: ignore
13-
14-
if TYPE_CHECKING:
15-
from ply.lex import LexToken
11+
from ply.lex import lex, LexToken # type: ignore
1612

1713

1814
class SieveLexer():
1915

20-
def __init__(self) -> None:
21-
self.lexer = ply.lex.lex(
16+
def __init__(self, debug: bool = False) -> None:
17+
self.lexer = lex(
2218
module=self,
23-
debug=False
19+
debug=debug
2420
)
2521
self.lexer.linestart = 0
2622

0 commit comments

Comments
 (0)