This commit is contained in:
Edgar 2023-12-14 11:38:33 +01:00
parent 86013c13e5
commit 6714ce129c
No known key found for this signature in database
GPG key ID: 70ADAE8F35904387
3 changed files with 74 additions and 2 deletions

View file

@ -16,3 +16,47 @@ pub struct ConstantDef<'a> {
pub ident: &'a str,
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>,
}

View file

@ -18,6 +18,7 @@ extern {
"string" => Token::String(<&'input str>),
"-" => Token::SpecialMinus,
"+" => 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: true, ident },
}
pub ConstantDef: ast::ConstantDef<'input> = {
<ident:"identifier"> "=" <value:Constant> => ast::ConstantDef {
ident,
value
}
}

View file

@ -66,8 +66,28 @@ mod test {
check("2.2", ast::Constant::Number(ast::Number::Real("2.2")));
check("1e10", ast::Constant::Number(ast::Number::Real("1e10")));
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")),
},
);
}
}