-
Notifications
You must be signed in to change notification settings - Fork 9
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
lark syntax -> llguidance translator #37
Merged
Changes from 8 commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
e3f1bff
start on lark "importer"
mmoskal ce47dd7
draft parsing code
mmoskal 0a6887c
Merge branch 'main' into lark
mmoskal b47aea8
lark grammar parses
mmoskal abee01c
move lark to folder
mmoskal 446a292
split lark
mmoskal 7b279f3
start on compiler
mmoskal 6fecfdb
fix zero_or_more()
mmoskal 6a39e60
basic lark translation working
mmoskal 9c9ab24
add llg_new_constraint_lark
mmoskal 075714b
add support for json_schema and lark_grammar in GrammarWithLexer
mmoskal bbd8e3c
add llg_new_constraint_any
mmoskal File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 |
---|---|---|
@@ -1,6 +1,6 @@ | ||
mod from_guidance; | ||
mod grammar; | ||
mod lexer; | ||
pub(crate) mod lexer; | ||
mod parser; | ||
|
||
pub mod lexerspec; | ||
|
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,97 @@ | ||
/// Represents an item in the grammar (rule, token, or statement). | ||
#[derive(Debug, Clone)] | ||
pub enum Item { | ||
Rule(Rule), | ||
Token(TokenDef), | ||
Statement(Statement), | ||
} | ||
|
||
/// Represents a grammar rule. | ||
#[derive(Debug, Clone)] | ||
pub struct Rule { | ||
pub name: String, | ||
pub params: Option<RuleParams>, | ||
pub priority: Option<i32>, | ||
pub expansions: Expansions, | ||
} | ||
|
||
/// Represents a token definition. | ||
#[derive(Debug, Clone)] | ||
pub struct TokenDef { | ||
pub name: String, | ||
pub params: Option<TokenParams>, | ||
pub priority: Option<i32>, | ||
pub expansions: Expansions, | ||
} | ||
|
||
/// Represents different types of statements. | ||
#[derive(Debug, Clone)] | ||
pub enum Statement { | ||
Ignore(Expansions), | ||
Import { | ||
path: ImportPath, | ||
alias: Option<String>, | ||
}, | ||
MultiImport { | ||
path: ImportPath, | ||
names: Vec<String>, | ||
}, | ||
OverrideRule(Box<Rule>), | ||
Declare(Vec<String>), | ||
} | ||
|
||
/// Represents an import path. | ||
#[derive(Debug, Clone)] | ||
pub struct ImportPath(pub Vec<String>); | ||
|
||
/// Represents parameters for a rule. | ||
#[derive(Debug, Clone)] | ||
pub struct RuleParams(pub Vec<String>); | ||
|
||
/// Represents parameters for a token. | ||
#[derive(Debug, Clone)] | ||
pub struct TokenParams(pub Vec<String>); | ||
|
||
/// Represents a list of expansions. | ||
#[derive(Debug, Clone)] | ||
pub struct Expansions(pub Vec<Alias>); | ||
|
||
/// Represents an alias in the grammar. | ||
#[derive(Debug, Clone)] | ||
pub struct Alias { | ||
pub expansion: Expansion, | ||
pub alias: Option<String>, | ||
} | ||
|
||
/// Represents an expansion consisting of expressions. | ||
#[derive(Debug, Clone)] | ||
pub struct Expansion(pub Vec<Expr>); | ||
|
||
/// Represents an expression. | ||
#[derive(Debug, Clone)] | ||
pub struct Expr { | ||
pub atom: Atom, | ||
pub op: Option<Op>, | ||
pub range: Option<(i32, i32)>, | ||
} | ||
|
||
/// Represents an atom in the grammar. | ||
#[derive(Debug, Clone)] | ||
pub enum Atom { | ||
Group(Expansions), | ||
Maybe(Expansions), | ||
Value(Value), | ||
} | ||
|
||
/// Represents different values in the grammar. | ||
#[derive(Debug, Clone)] | ||
pub enum Value { | ||
LiteralRange(String, String), | ||
Name(String), | ||
Literal(String), | ||
TemplateUsage { name: String, values: Vec<Value> }, | ||
} | ||
|
||
/// Represents an operator. | ||
#[derive(Debug, Clone)] | ||
pub struct Op(pub String); |
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(()) | ||
} | ||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@hudson-ai be aware that this was broken; I'll be merging this initial lark stuff soon, so the fix will come in (zero_or_more was really zero_or_one)