Skip to content
This repository was archived by the owner on Mar 31, 2026. It is now read-only.

Commit 797a384

Browse files
committed
test: add lrparser rust implementation
1 parent 0b90942 commit 797a384

5 files changed

Lines changed: 421 additions & 147 deletions

File tree

ply_parser/ply/lrparser.py

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,15 @@
2828
error_count: Final = 3 # Number of symbols that must be shifted to leave recovery mode
2929

3030

31+
try:
32+
from rs_ply import LRParser as RustLRParser
33+
from rs_ply import StateMachine as RustStateMachine
34+
35+
HAS_RS_PLY = True
36+
except ImportError:
37+
HAS_RS_PLY = False
38+
39+
3140
@dataclass(slots=True)
3241
class LexToken:
3342
"""keep for backward compatibility and name distinction"""
@@ -573,9 +582,16 @@ def expect_goto(self, state: int, sym: str) -> int:
573582
return self.gotos[state][sym]
574583

575584

576-
def load_parser(parser_table: Path | str, module: ParserProtocol) -> LRParser:
585+
def load_parser(parser_table: Path | str, module: ParserProtocol) -> Union[LRParser, "RustLRParser"]:
577586
if isinstance(parser_table, Path):
578587
parser_table = str(parser_table)
579588

589+
if HAS_RS_PLY and parser_table.endswith(".jsonl"):
590+
try:
591+
fsm = RustStateMachine.new_from_file(parser_table)
592+
return RustLRParser(fsm, module, errorf=getattr(module, "p_error", None))
593+
except Exception:
594+
pass
595+
580596
fsm = StateMachine(parser_table)
581597
return LRParser(fsm, errorf=getattr(module, "p_error", None), module=module)

rs-ply/src/data.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -92,20 +92,20 @@ impl StateMachine {
9292
})
9393
}
9494

95-
fn get_default_action(&self, state: u16) -> Option<i16> {
95+
pub fn get_default_action(&self, state: u16) -> Option<i16> {
9696
self.defaults.get(&state).copied()
9797
}
9898

99-
fn get_action(&self, state: usize, sym: &str) -> Option<i16> {
99+
pub fn get_action(&self, state: usize, sym: &str) -> Option<i16> {
100100
let symbols = self.actions.get(state).unwrap();
101101
let action = symbols.get(sym);
102102
action.map(|x| *x)
103103
}
104-
fn expect_production(&self, index: usize) -> Production {
104+
pub fn expect_production(&self, index: usize) -> Production {
105105
let prod = self.productions.get(index).unwrap();
106106
prod.clone()
107107
}
108-
fn expect_goto(&self, state: usize, sym: &str) -> PyResult<u16> {
108+
pub fn expect_goto(&self, state: usize, sym: &str) -> PyResult<u16> {
109109
let gotos = self
110110
.gotos
111111
.get(state)

rs-ply/src/lib.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@ use pyo3::prelude::*;
77
/// A Python module implemented in Rust.
88
#[pymodule]
99
fn rs_ply(m: &Bound<'_, PyModule>) -> PyResult<()> {
10-
m.add_class::<data::StateMachine>()?;
10+
m.add_class::<data::Production>()?;
11+
m.add_class::<yacc_types::YaccSymbol>()?;
12+
m.add_class::<yacc_types::YaccProduction>()?;
13+
m.add_class::<lrparser::LRParser>()?;
1114
Ok(())
1215
}

0 commit comments

Comments
 (0)