Skip to content

Commit 78bb899

Browse files
committed
Modified C parser to have access to translation context during parsing
1 parent c675b53 commit 78bb899

File tree

4 files changed

+40
-26
lines changed

4 files changed

+40
-26
lines changed

src/c/parser/expr.rs

+5-2
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,14 @@ use crate::{
88
parser::speculate::speculate,
99
punctuator::Punctuator,
1010
token::CTokenKind,
11-
translate::get_decorators,
11+
translate::TranslateCtx,
1212
},
1313
source_files::Source,
1414
};
1515
use ignore::types::TypesBuilder;
1616

1717
// Implements expression parsing for the C parser
18-
impl<'a> Parser<'a> {
18+
impl<'input, 'diagnostics> Parser<'input, 'diagnostics> {
1919
pub fn parse_expr_singular(&mut self) -> Result<Expr, ParseError> {
2020
let primary = self.parse_expr_primary()?;
2121
self.parse_operator_expr(0, primary)
@@ -269,7 +269,10 @@ impl<'a> Parser<'a> {
269269
CTokenKind::SizeofKeyword => {
270270
self.input.advance();
271271

272+
#[allow(unused)]
272273
if let Ok(type_name) = speculate!(self.input, self.parse_type_in_parens()) {
274+
let ctx =
275+
TranslateCtx::new(&mut self.ast_file, &mut self.typedefs, self.diagnostics);
273276
let mut type_builder = TypesBuilder::new();
274277
return todo!("handle parsed sizeof(type)");
275278
}

src/c/parser/mod.rs

+14-18
Original file line numberDiff line numberDiff line change
@@ -34,26 +34,21 @@ use crate::{
3434
};
3535
use std::collections::HashMap;
3636

37-
pub struct Parser<'a> {
38-
input: Input<'a>,
39-
typedefs: HashMap<String, CTypedef>,
37+
pub struct Parser<'input, 'diagnostics> {
38+
input: Input<'input>,
39+
pub ast_file: AstFile,
40+
pub typedefs: HashMap<String, CTypedef>,
41+
pub diagnostics: &'diagnostics Diagnostics<'diagnostics>,
4042
enum_constants: HashMap<String, Integer>,
41-
diagnostics: &'a Diagnostics<'a>,
4243
c_file_type: CFileType,
4344
}
4445

45-
impl Parser<'_> {
46-
pub fn typedefs(&self) -> &HashMap<String, CTypedef> {
47-
&self.typedefs
48-
}
49-
50-
pub fn typedefs_mut(&mut self) -> &mut HashMap<String, CTypedef> {
51-
&mut self.typedefs
52-
}
53-
}
54-
55-
impl<'a> Parser<'a> {
56-
pub fn new(input: Input<'a>, diagnostics: &'a Diagnostics<'a>, c_file_type: CFileType) -> Self {
46+
impl<'input, 'diagnostics> Parser<'input, 'diagnostics> {
47+
pub fn new(
48+
input: Input<'input>,
49+
diagnostics: &'diagnostics Diagnostics<'diagnostics>,
50+
c_file_type: CFileType,
51+
) -> Self {
5752
let mut typedefs = HashMap::default();
5853

5954
diagnostics.push(WarningDiagnostic::new(
@@ -72,6 +67,7 @@ impl<'a> Parser<'a> {
7267
);
7368

7469
Self {
70+
ast_file: AstFile::new(),
7571
input,
7672
typedefs,
7773
enum_constants: HashMap::default(),
@@ -84,7 +80,7 @@ impl<'a> Parser<'a> {
8480
self.input.switch_input(tokens);
8581
}
8682

87-
pub fn parse(&mut self) -> Result<AstFile, ParseError> {
83+
pub fn parse(&mut self) -> Result<(), ParseError> {
8884
let mut ast_file = AstFile::new();
8985

9086
while !self.input.peek().is_end_of_file() {
@@ -141,7 +137,7 @@ impl<'a> Parser<'a> {
141137
}
142138
}
143139

144-
Ok(ast_file)
140+
Ok(())
145141
}
146142

147143
fn parse_external_declaration(&mut self) -> Result<ExternalDeclaration, ParseError> {

src/c/translate/mod.rs

+16-1
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,29 @@ use crate::{
1515
workspace::compile::c_code::CFileType,
1616
};
1717
use std::collections::HashMap;
18-
pub use types::get_decorators;
1918

2019
pub struct TranslateCtx<'ast, 'typedefs, 'diagnostics, 'source_files> {
2120
pub ast_file: &'ast mut AstFile,
2221
pub typedefs: &'typedefs mut HashMap<String, CTypedef>,
2322
pub diagnostics: &'diagnostics Diagnostics<'source_files>,
2423
}
2524

25+
impl<'ast, 'typedefs, 'diagnostics, 'source_files>
26+
TranslateCtx<'ast, 'typedefs, 'diagnostics, 'source_files>
27+
{
28+
pub fn new<'input>(
29+
ast_file: &'ast mut AstFile,
30+
typedefs: &'typedefs mut HashMap<String, CTypedef>,
31+
diagnostics: &'diagnostics Diagnostics<'source_files>,
32+
) -> Self {
33+
Self {
34+
ast_file,
35+
typedefs,
36+
diagnostics,
37+
}
38+
}
39+
}
40+
2641
pub fn declare_named_declaration(
2742
ctx: &mut TranslateCtx,
2843
declarator: &Declarator,

src/workspace/compile/c_code.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ pub fn c_code(
4848
c_file_type,
4949
);
5050

51-
let mut ast_file = parser.parse().map_err(into_show)?;
51+
parser.parse().map_err(into_show)?;
5252

5353
// Translate preprocessor #define object macros
5454
for (define_name, define) in &defines {
@@ -59,14 +59,14 @@ pub fn c_code(
5959
if let Ok(value) = parser.parse_expr_singular().and_then(|expr| {
6060
translate_expr(
6161
&mut TranslateCtx {
62-
ast_file: &mut ast_file,
63-
typedefs: parser.typedefs_mut(),
62+
ast_file: &mut parser.ast_file,
63+
typedefs: &mut parser.typedefs,
6464
diagnostics: compiler.diagnostics,
6565
},
6666
&expr,
6767
)
6868
}) {
69-
ast_file.helper_exprs.push(ast::HelperExpr {
69+
parser.ast_file.helper_exprs.push(ast::HelperExpr {
7070
name: define_name.clone(),
7171
value,
7272
source: define.source,
@@ -79,5 +79,5 @@ pub fn c_code(
7979
}
8080
}
8181

82-
Ok(ast_file)
82+
Ok(parser.ast_file)
8383
}

0 commit comments

Comments
 (0)