|
6 | 6 | from sifter.grammar.command import Command |
7 | 7 | from typing import ( |
8 | 8 | Any, |
9 | | - TYPE_CHECKING, |
10 | 9 | cast, |
11 | 10 | Text |
12 | 11 | ) |
13 | 12 |
|
14 | | -import ply.yacc # type: ignore |
| 13 | +from ply.yacc import yacc, NullLogger, YaccError, LRParser, YaccProduction # type: ignore |
15 | 14 |
|
16 | 15 | from sifter.grammar.tag import Tag |
17 | 16 | from sifter.grammar.command_list import CommandList |
18 | 17 | from sifter.grammar.string import String |
19 | 18 | from sifter.grammar.lexer import SieveLexer |
20 | 19 | from sifter.extensions import ExtensionRegistry |
21 | 20 |
|
22 | | -if TYPE_CHECKING: |
23 | | - from py.yacc import LRParser, YaccProduction # type: ignore |
24 | | - |
25 | 21 |
|
26 | 22 | class SieveParser(): |
27 | 23 |
|
28 | 24 | tokens = SieveLexer.tokens |
29 | 25 |
|
30 | | - def __init__(self) -> None: |
31 | | - self.lexer = SieveLexer() |
| 26 | + def __init__(self, debug: bool = False) -> None: |
| 27 | + self.lexer = SieveLexer(debug=debug) |
32 | 28 | self.parser = self.make_parser(self) |
33 | 29 | self.extensions = ExtensionRegistry() |
34 | 30 |
|
35 | 31 | @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( |
38 | 34 | module=mod, |
39 | | - debug=False, |
40 | | - write_tables=False |
| 35 | + debug=debug, |
| 36 | + write_tables=False, |
| 37 | + errorlog=NullLogger() if not debug else None |
41 | 38 | ) |
42 | 39 |
|
43 | 40 | def parse(self, rules: Text, tracking: int = 0) -> CommandList: |
44 | 41 | self.parser.errok() |
45 | 42 |
|
46 | 43 | rules = self.parser.parse(rules, self.lexer, tracking=tracking) |
47 | 44 | if not self.parser.errorok: |
48 | | - raise ply.yacc.YaccError('Syntax error') |
| 45 | + raise YaccError('Syntax error') |
49 | 46 |
|
50 | 47 | return cast(CommandList, rules) |
51 | 48 |
|
@@ -187,3 +184,6 @@ def p_stringlist_single(self, p: 'YaccProduction') -> None: |
187 | 184 | def p_string(self, p: 'YaccProduction') -> None: |
188 | 185 | """string : QUOTED_STRING""" |
189 | 186 | p[0] = String(p[1]) |
| 187 | + |
| 188 | + def p_error(self, p: 'YaccProduction') -> None: |
| 189 | + pass |
0 commit comments