mirror of
https://github.com/edg-l/PascalMLIR.git
synced 2024-10-18 06:53:47 +00:00
wip
This commit is contained in:
parent
86013c13e5
commit
6714ce129c
44
src/ast.rs
44
src/ast.rs
|
@ -16,3 +16,47 @@ pub struct ConstantDef<'a> {
|
||||||
pub ident: &'a str,
|
pub ident: &'a str,
|
||||||
pub value: Constant<'a>,
|
pub value: Constant<'a>,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[derive(Debug, Clone, PartialEq, Eq)]
|
||||||
|
pub enum SimpleType {
|
||||||
|
Integer,
|
||||||
|
Real,
|
||||||
|
Boolean,
|
||||||
|
Char,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Debug, Clone, PartialEq, Eq)]
|
||||||
|
pub enum RecordField<'input> {
|
||||||
|
Fixed {
|
||||||
|
identifier_list: Vec<&'input str>,
|
||||||
|
type_denoter: Type<'input>,
|
||||||
|
},
|
||||||
|
Case {
|
||||||
|
// TODO: 6.4.3.3 Record-types
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Debug, Clone, PartialEq, Eq)]
|
||||||
|
pub enum Type<'input> {
|
||||||
|
Identifier(&'input str),
|
||||||
|
Simple(SimpleType),
|
||||||
|
Enumerated(Vec<&'input str>),
|
||||||
|
Subrange {
|
||||||
|
start: Constant<'input>,
|
||||||
|
end: Constant<'input>,
|
||||||
|
},
|
||||||
|
Array {
|
||||||
|
index: Vec<Type<'input>>,
|
||||||
|
component: Box<Type<'input>>,
|
||||||
|
packed: bool,
|
||||||
|
},
|
||||||
|
Record {
|
||||||
|
fields: Vec<RecordField<'input>>,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Debug, Clone, PartialEq, Eq)]
|
||||||
|
pub struct TypeDef<'input> {
|
||||||
|
pub ident: &'input str,
|
||||||
|
pub value: Type<'input>,
|
||||||
|
}
|
||||||
|
|
|
@ -18,6 +18,7 @@ extern {
|
||||||
"string" => Token::String(<&'input str>),
|
"string" => Token::String(<&'input str>),
|
||||||
"-" => Token::SpecialMinus,
|
"-" => Token::SpecialMinus,
|
||||||
"+" => Token::SpecialPlus,
|
"+" => Token::SpecialPlus,
|
||||||
|
"=" => Token::SpecialEqual,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -42,3 +43,10 @@ pub Constant: ast::Constant<'input> = {
|
||||||
"+"? <ident:"identifier"> => ast::Constant::Identifier { is_negative: false, ident },
|
"+"? <ident:"identifier"> => ast::Constant::Identifier { is_negative: false, ident },
|
||||||
"-" <ident:"identifier"> => ast::Constant::Identifier { is_negative: true, ident },
|
"-" <ident:"identifier"> => ast::Constant::Identifier { is_negative: true, ident },
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub ConstantDef: ast::ConstantDef<'input> = {
|
||||||
|
<ident:"identifier"> "=" <value:Constant> => ast::ConstantDef {
|
||||||
|
ident,
|
||||||
|
value
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
24
src/main.rs
24
src/main.rs
|
@ -66,8 +66,28 @@ mod test {
|
||||||
check("2.2", ast::Constant::Number(ast::Number::Real("2.2")));
|
check("2.2", ast::Constant::Number(ast::Number::Real("2.2")));
|
||||||
check("1e10", ast::Constant::Number(ast::Number::Real("1e10")));
|
check("1e10", ast::Constant::Number(ast::Number::Real("1e10")));
|
||||||
|
|
||||||
|
|
||||||
check(r#""hello world""#, ast::Constant::String("\"hello world\""));
|
check(r#""hello world""#, ast::Constant::String("\"hello world\""));
|
||||||
check(r#""\"hell\"o world""#, ast::Constant::String("\"\\\"hell\\\"o world\""));
|
check(
|
||||||
|
r#""\"hell\"o world""#,
|
||||||
|
ast::Constant::String("\"\\\"hell\\\"o world\""),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn parse_constant_definition() {
|
||||||
|
#[track_caller]
|
||||||
|
fn check(input: &str, value: ast::ConstantDef) {
|
||||||
|
let lexer = Lexer::new(input);
|
||||||
|
let parser = grammar::ConstantDefParser::new();
|
||||||
|
assert_eq!(parser.parse("", lexer).unwrap(), value)
|
||||||
|
}
|
||||||
|
|
||||||
|
check(
|
||||||
|
"MYIDENTIFIER = 2.2",
|
||||||
|
ast::ConstantDef {
|
||||||
|
ident: "MYIDENTIFIER",
|
||||||
|
value: ast::Constant::Number(ast::Number::Real("2.2")),
|
||||||
|
},
|
||||||
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue