-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Parser successfully parsing each line and moving strings to string ta…
…ble.
- Loading branch information
Showing
11 changed files
with
166 additions
and
3 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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 |
---|---|---|
|
@@ -2,6 +2,8 @@ | |
resolver = "2" | ||
members = [ | ||
"cli", | ||
"common", | ||
"compat", | ||
"parser", | ||
] | ||
|
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
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,6 @@ | ||
[package] | ||
name = "cuentitos-common" | ||
version = "0.3.0" | ||
edition = "2021" | ||
|
||
[dependencies] |
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,12 @@ | ||
pub type StringId = usize; | ||
|
||
#[derive(Debug, Clone)] | ||
pub enum Block { | ||
String(StringId) | ||
} | ||
|
||
#[derive(Debug, Default, Clone)] | ||
pub struct Database { | ||
pub blocks: Vec<Block>, | ||
pub strings: Vec<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,37 @@ | ||
# Parser | ||
|
||
### Submitters | ||
|
||
- Fran Tufro | ||
|
||
## Change Log | ||
|
||
- 2025-01-20 - [First Draft of ADR created](https://github.com/hiddenpeopleclub/cuentitos/pull/52). | ||
|
||
## Context | ||
|
||
Cuentitos needs a fast and efficient parser to define the language grammar and | ||
generate the necessary structures to be executed at runtime. | ||
|
||
In version 0.2 we used PEST, a parser generator for Rust that allowed us to | ||
define the language grammar and generate a parser that was responsible for | ||
transforming the source code into a data structure that we could easily translate | ||
into Rust code. | ||
|
||
The main problem with PEST is that it was not easy to modify and extend the | ||
language grammar, and it was not easy to maintain. | ||
|
||
Thinking about the possibility of extending the language in the future through | ||
plugins, it is necessary that the parser be easy to extend and maintain. | ||
|
||
## Proposed Design | ||
|
||
I will start by implementing a very simple parser: it will go line by line | ||
through the script, and asking a list of possible classes if they can parse the | ||
line. If a class can parse the line, the class is responsible for parsing it and | ||
returning an object that represents the line. | ||
|
||
## Decision | ||
|
||
## Other Related ADRs | ||
|
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,7 @@ | ||
[package] | ||
name = "cuentitos-parser" | ||
version = "0.3.0" | ||
edition = "2021" | ||
|
||
[dependencies] | ||
cuentitos-common = { path = "../common" } |
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,12 @@ | ||
use cuentitos_common::*; | ||
|
||
mod parser; | ||
use parser::*; | ||
|
||
mod line_parser; | ||
|
||
|
||
pub fn parse(script: &str) -> Result<Database, ParseError> { | ||
let mut parser = Parser::default(); | ||
parser.parse(script) | ||
} |
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,23 @@ | ||
use cuentitos_common::Block; | ||
use crate::ParseError; | ||
|
||
pub struct Line<'a> { | ||
pub parsed: bool, | ||
pub text: &'a str, | ||
} | ||
|
||
|
||
#[derive(Debug)] | ||
pub struct LineParserResult { | ||
pub block: Block, | ||
pub string: String, | ||
} | ||
|
||
pub fn parse(line: Line) -> Result<LineParserResult, ParseError> { | ||
Ok( | ||
LineParserResult { | ||
block: Block::String(1), | ||
string: String::from(line.text), | ||
} | ||
) | ||
} |
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,40 @@ | ||
use crate::line_parser; | ||
use cuentitos_common::*; | ||
|
||
#[derive(Debug, Default)] | ||
pub struct Parser; | ||
|
||
#[derive(Debug)] | ||
#[derive(Clone)] | ||
pub enum ParseError { | ||
UnexpectedToken, | ||
UnexpectedEndOfFile, | ||
} | ||
|
||
|
||
|
||
impl Parser { | ||
pub fn parse<A>(&mut self, script: A) -> Result<Database, ParseError> | ||
where A: AsRef<str> | ||
{ | ||
let mut database = Database::default(); | ||
|
||
let script = script.as_ref(); | ||
|
||
// iterate through each line | ||
for line in script.lines() { | ||
let line = line_parser::Line { parsed: false, text: line }; | ||
let result = line_parser::parse(line); | ||
|
||
match result { | ||
Ok(result) => { | ||
database.blocks.push(Block::String(database.strings.len())); | ||
database.strings.push(result.string); | ||
}, | ||
Err(_) => panic!("Error parsing line"), | ||
} | ||
} | ||
|
||
Ok(database) | ||
} | ||
} |