Skip to content

Commit

Permalink
Parser successfully parsing each line and moving strings to string ta…
Browse files Browse the repository at this point in the history
…ble.
  • Loading branch information
frantufro committed Jan 20, 2025
1 parent ac9fb4c commit d1886f0
Show file tree
Hide file tree
Showing 11 changed files with 166 additions and 3 deletions.
13 changes: 13 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
resolver = "2"
members = [
"cli",
"common",
"compat",
"parser",
]

2 changes: 2 additions & 0 deletions cli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,5 @@ edition = "2021"

[dependencies]
clap = { version = "4.5.23", features = ["derive"] }
cuentitos-common = { path = "../common" }
cuentitos-parser = { path = "../parser" }
15 changes: 12 additions & 3 deletions cli/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use std::fs::File;
use std::path::PathBuf;
use clap::{Parser, Subcommand};
/// Simple program to greet a person
Expand All @@ -24,9 +25,17 @@ fn main() {
let cli = Args::parse();

match cli.command {
Commands::Run { script_path: _, input_string: _ } => {
println!("This is a single line");
println!("END");
Commands::Run { script_path, input_string } => {

// Read the script file
let script = std::fs::read_to_string(script_path).unwrap();

// Parse it
let database = cuentitos_parser::parse(&script).unwrap();
println!("{:?}", database);

// println!("This is a single line");
// println!("END");
// println!("Running script: {:?}", script_path);
// if let Some(input) = input_string {
// println!("Input string: {}", input);
Expand Down
6 changes: 6 additions & 0 deletions common/Cargo.toml
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]
12 changes: 12 additions & 0 deletions common/src/lib.rs
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>
}
37 changes: 37 additions & 0 deletions docs/architecture/000004-parser.md
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

7 changes: 7 additions & 0 deletions parser/Cargo.toml
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" }
12 changes: 12 additions & 0 deletions parser/src/lib.rs
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)
}
23 changes: 23 additions & 0 deletions parser/src/line_parser/mod.rs
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),
}
)
}
40 changes: 40 additions & 0 deletions parser/src/parser.rs
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)
}
}

0 comments on commit d1886f0

Please sign in to comment.