Skip to content

Commit

Permalink
work in progress parser
Browse files Browse the repository at this point in the history
  • Loading branch information
edg-l committed Jan 5, 2024
1 parent 133da04 commit 685cef2
Show file tree
Hide file tree
Showing 5 changed files with 113 additions and 7 deletions.
9 changes: 8 additions & 1 deletion crates/concrete_ast/src/common.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,15 @@
#[derive(Debug, Clone, Copy)]
#[derive(Debug, Clone, Copy, Hash, PartialEq, Eq, PartialOrd, Ord)]
pub struct Span {
pub from: usize,
pub to: usize,
}

impl Span {
pub fn new(from: usize, to: usize) -> Self {
Self { from, to }
}
}

#[derive(Clone, Debug, Eq, Hash, PartialEq)]
pub struct DocString {
contents: String,
Expand All @@ -12,6 +18,7 @@ pub struct DocString {
#[derive(Clone, Debug, Eq, Hash, PartialEq)]
pub struct Ident {
pub name: String,
pub span: Span,
}

#[derive(Clone, Debug, Eq, Hash, PartialEq)]
Expand Down
2 changes: 1 addition & 1 deletion crates/concrete_ast/src/constants.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,6 @@ pub struct ConstantDecl {

#[derive(Clone, Debug, PartialEq)]
pub struct ConstantDef {
decl: ConstantDecl,
pub decl: ConstantDecl,
pub value: Expression,
}
82 changes: 79 additions & 3 deletions crates/concrete_parser/src/grammar.lalrpop
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
use crate::tokens::Token;
use crate::lexer::LexicalError;
use concrete_ast as ast;
use std::str::FromStr;

grammar;

Expand All @@ -24,7 +26,8 @@ extern {

// literals
"identifier" => Token::Identifier(<String>),
"integer" => Token::String(<String>),
"integer" => Token::Integer(<u64>),
"string" => Token::String(<String>),
"boolean" => Token::Boolean(<bool>),

// Other
Expand Down Expand Up @@ -57,6 +60,79 @@ extern {
}
}

pub Term: () = {
"identifier" => (),
Comma<T>: Vec<T> = {
<mut v:(<T> ",")*> <e:T?> => match e {
None => v,
Some(e) => {
v.push(e);
v
}
}
};

SemiColonSeparated<T>: Vec<T> = {
<T> ";" => vec![<>],
<mut s:SemiColonSeparated<T>> ";" <n:T> ";" => {
s.push(n);
s
}
};

pub(crate) Ident: ast::common::Ident = {
<lo:@L> <name:"identifier"> <hi:@R> => ast::common::Ident {
name,
span: ast::common::Span::new(lo, hi),
}
}

pub Program: Vec<ast::modules::ModuleBase> = {
<ModuleBase> => vec![<>],
<mut s:Program> <n:ModuleBase> => {
s.push(n);
s
},
}

pub(crate) ModuleBase: ast::modules::ModuleBase = {
"mod" <name:Ident> "{" <contents:SemiColonSeparated<ModuleDefItem>> "}" => {
ast::modules::ModuleBase {
doc_string: None,
imports: vec![], // todo: add imports
name,
contents
}
}
}

pub(crate) ModuleDefItem: ast::modules::ModuleDefItem = {
"const" <name:Ident> ":" <type_spec:TypeSpec> "=" <exp:Expression> => {
ast::modules::ModuleDefItem::Constant(ast::constants::ConstantDef {
decl: ast::constants::ConstantDecl {
doc_string: None,
name,
r#type: type_spec
},
value: exp,
})
}
}

pub(crate) TypeSpec: ast::types::TypeSpec = {
<name:Ident> => ast::types::TypeSpec::Simple {
name
},
<name:Ident> "<" <type_params:Comma<TypeSpec>> ">" => ast::types::TypeSpec::Generic {
name,
type_params
}
}

pub(crate) Expression: ast::expressions::Expression = {
<AtomicExpr> => ast::expressions::Expression::Atomic(<>),
}

pub(crate) AtomicExpr: ast::expressions::AtomicExpr = {
<"integer"> => ast::expressions::AtomicExpr::ConstInt(<>),
<"boolean"> => ast::expressions::AtomicExpr::ConstBool(<>),
<"string"> => ast::expressions::AtomicExpr::ConstStr(<>),
}
22 changes: 22 additions & 0 deletions crates/concrete_parser/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,26 @@
#![allow(unused)]

use lalrpop_util::lalrpop_mod;

mod lexer;
pub mod tokens;

lalrpop_mod!(pub grammar);

#[cfg(test)]
mod tests {
use crate::{grammar, lexer::Lexer};

#[test]
fn parse_program() {
let source = r##"
mod ModuleName {
const MY_CONSTANT: u8 = 2;
}
"##;
let lexer = Lexer::new(source);
let parser = grammar::ProgramParser::new();
let mut ast = parser.parse(lexer).unwrap();
dbg!(ast);
}
}
5 changes: 3 additions & 2 deletions crates/concrete_parser/src/tokens.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use logos::Logos;
use std::str::FromStr;
use std::{convert::Infallible, fmt}; // to implement the Display trait

#[derive(Debug, PartialEq, Clone, Default)]
Expand Down Expand Up @@ -53,8 +54,8 @@ pub enum Token {
Identifier(String),

// Literals
#[regex(r"\d+", |lex| lex.slice().to_string())]
Integer(String),
#[regex(r"\d+", |lex| lex.slice().parse::<u64>().unwrap())]
Integer(u64),
#[regex(r#""(?:[^"]|\\")*""#, |lex| lex.slice().to_string())]
String(String),
#[regex(r"(true|false)", |lex| lex.slice().parse::<bool>().unwrap())]
Expand Down

0 comments on commit 685cef2

Please sign in to comment.