can parse global variable declarations

This commit is contained in:
nora 2022-06-25 19:09:27 +02:00
parent f53c054a9a
commit 4b646b9128
5 changed files with 189 additions and 74 deletions

View file

@ -371,10 +371,20 @@ where
span.extend(span2),
))
} else {
expect!(self, Tok::Punct(Punct::Semicolon));
Ok((ExternalDecl::Decl(declaration), span))
}
}
fn external_declarations(&mut self) -> Result<Vec<Spanned<ExternalDecl>>> {
let mut decls = Vec::new();
while self.peek_t().is_ok() {
let decl = self.external_declaration()?;
decls.push(decl);
}
Ok(decls)
}
fn function_param_declaration_list(&mut self) -> Result<FunctionParams> {
// If the declarator includes a parameter type list, the declaration of each parameter shall
// include an identifier, except for the special case of a parameter list consisting of a single

View file

@ -3,42 +3,44 @@ source: parser/src/parser/tests.rs
expression: parsed
---
Ok(
(
FunctionDef(
FunctionDef {
declaration: Normal(
NormalDecl {
decl_spec: DeclSpec {
ty: Int,
attrs: DeclAttr {
is_extern: true,
is_static: false,
is_thread_local: true,
},
},
init_declarators: [
(
InitDecl {
declarator: Declarator {
decl: WithParams {
ident: (
"uwu",
35..38,
),
params: [],
},
pointer: false,
},
init: None,
[
(
FunctionDef(
FunctionDef {
declaration: Normal(
NormalDecl {
decl_spec: DeclSpec {
ty: Int,
attrs: DeclAttr {
is_extern: true,
is_static: false,
is_thread_local: true,
},
35..38,
),
],
},
),
body: [],
},
},
init_declarators: [
(
InitDecl {
declarator: Declarator {
decl: WithParams {
ident: (
"uwu",
35..38,
),
params: [],
},
pointer: false,
},
init: None,
},
35..38,
),
],
},
),
body: [],
},
),
1..43,
),
1..43,
),
],
)

View file

@ -3,42 +3,44 @@ source: parser/src/parser/tests.rs
expression: parsed
---
Ok(
(
FunctionDef(
FunctionDef {
declaration: Normal(
NormalDecl {
decl_spec: DeclSpec {
ty: Void,
attrs: DeclAttr {
is_extern: false,
is_static: false,
is_thread_local: false,
},
},
init_declarators: [
(
InitDecl {
declarator: Declarator {
decl: WithParams {
ident: (
"uwu",
6..9,
),
params: [],
},
pointer: false,
},
init: None,
[
(
FunctionDef(
FunctionDef {
declaration: Normal(
NormalDecl {
decl_spec: DeclSpec {
ty: Void,
attrs: DeclAttr {
is_extern: false,
is_static: false,
is_thread_local: false,
},
6..9,
),
],
},
),
body: [],
},
},
init_declarators: [
(
InitDecl {
declarator: Declarator {
decl: WithParams {
ident: (
"uwu",
6..9,
),
params: [],
},
pointer: false,
},
init: None,
},
6..9,
),
],
},
),
body: [],
},
),
1..14,
),
1..14,
),
],
)

View file

@ -0,0 +1,91 @@
---
source: parser/src/parser/tests.rs
expression: parsed
---
Ok(
[
(
Decl(
Normal(
NormalDecl {
decl_spec: DeclSpec {
ty: Int,
attrs: DeclAttr {
is_extern: false,
is_static: false,
is_thread_local: false,
},
},
init_declarators: [
(
InitDecl {
declarator: Declarator {
decl: Ident(
(
"test",
5..9,
),
),
pointer: false,
},
init: None,
},
5..9,
),
],
},
),
),
1..9,
),
(
Decl(
Normal(
NormalDecl {
decl_spec: DeclSpec {
ty: Double,
attrs: DeclAttr {
is_extern: false,
is_static: false,
is_thread_local: true,
},
},
init_declarators: [
(
InitDecl {
declarator: Declarator {
decl: Ident(
(
"uwu",
32..35,
),
),
pointer: false,
},
init: None,
},
32..35,
),
(
InitDecl {
declarator: Declarator {
decl: Ident(
(
"owo",
37..40,
),
),
pointer: false,
},
init: None,
},
37..40,
),
],
},
),
),
11..40,
),
],
)

View file

@ -15,7 +15,7 @@ fn the_current_root_parse_thing<'src>(src: impl Iterator<Item = (Tok<'src>, Span
lex: src.peekmore(),
};
parser.external_declaration()
parser.external_declarations()
}
macro_rules! parse_test {
@ -52,4 +52,14 @@ int uwu(long owo, unsigned qwq) {}
"#;
parse_test!(src);
}
}
#[test]
fn global_variable_declarations() {
let src = r#"
int test;
_Thread_local double uwu, owo;
"#;
parse_test!(src);
}