-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
79 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
use std::collections::HashMap; | ||
|
||
use anyhow::Result; | ||
|
||
use crate::{ | ||
api::{RegexSpec, TopLevelGrammar}, | ||
GrammarBuilder, NodeRef, | ||
}; | ||
|
||
use super::ast::*; | ||
|
||
struct Compiler { | ||
builder: GrammarBuilder, | ||
items: Vec<Item>, | ||
nodes: HashMap<String, NodeInfo>, | ||
} | ||
|
||
struct NodeInfo { | ||
id: NodeRef, | ||
is_terminal: bool, | ||
regex: Option<RegexSpec>, | ||
} | ||
|
||
pub fn lark_to_llguidance(items: Vec<Item>) -> Result<TopLevelGrammar> { | ||
let mut c = Compiler { | ||
builder: GrammarBuilder::new(), | ||
items, | ||
nodes: HashMap::new(), | ||
}; | ||
c.execute()?; | ||
c.builder.finalize() | ||
} | ||
|
||
impl Compiler { | ||
fn execute(&mut self) -> Result<()> { | ||
for item in self.items.iter() { | ||
match item { | ||
Item::Rule(rule) => { | ||
let id = self.builder.placeholder(); | ||
self.nodes.insert( | ||
rule.name.clone(), | ||
NodeInfo { | ||
id, | ||
is_terminal: false, | ||
regex: None, | ||
}, | ||
); | ||
} | ||
Item::Token(token_def) => { | ||
let id = self.builder.placeholder(); | ||
self.nodes.insert( | ||
token_def.name.clone(), | ||
NodeInfo { | ||
id, | ||
is_terminal: true, | ||
regex: None, | ||
}, | ||
); | ||
} | ||
Item::Statement(statement) => todo!(), | ||
} | ||
} | ||
Ok(()) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
mod ast; | ||
mod compiler; | ||
mod lexer; | ||
mod parser; | ||
|
||
|