Skip to content

Commit 84a67df

Browse files
committed
[refactor] centralize parser constants to improve maintainability
1 parent e244b01 commit 84a67df

5 files changed

Lines changed: 136 additions & 93 deletions

File tree

src/parser/mod.rs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,15 @@ pub mod parser_expr;
44
pub mod parser_stmt;
55
pub mod parser_type;
66
use nom::{
7-
branch::alt,
8-
bytes::complete::tag,
97
character::complete::{char, multispace0},
108
combinator::{map, opt},
11-
error::Error,
129
multi::separated_list0,
1310
sequence::tuple,
1411
IResult,
1512
};
1613

1714
use crate::ir::ast::Statement;
15+
use crate::parser::parser_common::SEMICOLON_CHAR;
1816

1917
pub use parser_expr::parse_expression;
2018
pub use parser_stmt::parse_statement;
@@ -25,10 +23,10 @@ pub fn parse(input: &str) -> IResult<&str, Vec<Statement>> {
2523
tuple((
2624
multispace0,
2725
separated_list0(
28-
tuple((multispace0, char(';'), multispace0)),
26+
tuple((multispace0, char(SEMICOLON_CHAR), multispace0)),
2927
parse_statement,
3028
),
31-
opt(tuple((multispace0, char(';')))), // optional trailing semicolon
29+
opt(tuple((multispace0, char(SEMICOLON_CHAR)))), // optional trailing semicolon
3230
multispace0,
3331
)),
3432
|(_, statements, _, _)| statements,

src/parser/parser_common.rs

Lines changed: 50 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,60 @@
11
use nom::{
22
branch::alt,
3-
bytes::complete::{tag, take_while},
4-
character::complete::{alpha1, char, digit1, multispace0},
5-
combinator::{map, map_res, not, opt, peek, recognize, value, verify},
6-
multi::{fold_many0, many0, separated_list0},
7-
sequence::{delimited, pair, preceded, terminated},
3+
bytes::complete::tag,
4+
character::complete::{alpha1, multispace0},
5+
combinator::{not, peek, recognize},
6+
multi::many0,
7+
sequence::{delimited, terminated},
88
IResult,
99
};
1010

1111
use crate::parser::keywords::KEYWORDS;
1212

13+
// Type name constants
14+
pub const INT_TYPE: &str = "Int";
15+
pub const REAL_TYPE: &str = "Real";
16+
pub const BOOLEAN_TYPE: &str = "Boolean";
17+
pub const STRING_TYPE: &str = "String";
18+
pub const UNIT_TYPE: &str = "Unit";
19+
pub const ANY_TYPE: &str = "Any";
20+
21+
// Special type constructor constants
22+
pub const MAYBE_TYPE: &str = "Maybe";
23+
pub const RESULT_TYPE: &str = "Result";
24+
25+
// Keyword constants
26+
pub const DATA_KEYWORD: &str = "data";
27+
pub const END_KEYWORD: &str = "end";
28+
29+
// Statement keyword constants
30+
pub const IF_KEYWORD: &str = "if";
31+
pub const ELSE_KEYWORD: &str = "else";
32+
pub const WHILE_KEYWORD: &str = "while";
33+
pub const FOR_KEYWORD: &str = "for";
34+
pub const IN_KEYWORD: &str = "in";
35+
pub const ASSERT_KEYWORD: &str = "assert";
36+
pub const DEF_KEYWORD: &str = "def";
37+
38+
// Operator and symbol constants
39+
pub const FUNCTION_ARROW: &str = "->";
40+
pub const PIPE_SYMBOL: &str = "|";
41+
pub const COLON_SYMBOL: &str = ":";
42+
pub const COMMA_SYMBOL: &str = ",";
43+
pub const SEMICOLON_SYMBOL: &str = ";";
44+
45+
// Bracket and parentheses constants
46+
pub const LEFT_BRACKET: char = '[';
47+
pub const RIGHT_BRACKET: char = ']';
48+
pub const LEFT_PAREN: char = '(';
49+
pub const RIGHT_PAREN: char = ')';
50+
51+
// Other character constants
52+
pub const COMMA_CHAR: char = ',';
53+
pub const COLON_CHAR: char = ':';
54+
pub const PIPE_CHAR: char = '|';
55+
pub const SEMICOLON_CHAR: char = ';';
56+
pub const EQUALS_CHAR: char = '=';
57+
1358
/// Accepts any character except '"' and control characters (like \n, \t)
1459
pub fn is_string_char(c: char) -> bool {
1560
c != '"' && !c.is_control()

src/parser/parser_expr.rs

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,13 @@ use nom::{
1212
use std::str::FromStr;
1313

1414
use crate::ir::ast::Expression;
15-
use crate::parser::parser_common::{identifier, is_string_char, keyword};
16-
17-
use crate::ir::ast::Function;
18-
use crate::ir::ast::Type;
19-
use crate::ir::ast::{Name, Statement, ValueConstructor};
20-
21-
use crate::parser::keywords::KEYWORDS;
15+
use crate::parser::parser_common::{
16+
identifier, is_string_char, keyword,
17+
// Bracket and parentheses constants
18+
LEFT_BRACKET, RIGHT_BRACKET, LEFT_PAREN, RIGHT_PAREN,
19+
// Other character constants
20+
COMMA_CHAR,
21+
};
2222

2323
pub fn parse_expression(input: &str) -> IResult<&str, Expression> {
2424
parse_or(input)
@@ -113,9 +113,9 @@ fn parse_factor(input: &str) -> IResult<&str, Expression> {
113113
parse_function_call,
114114
parse_var,
115115
delimited(
116-
char::<&str, Error<&str>>('('),
116+
char::<&str, Error<&str>>(LEFT_PAREN),
117117
parse_expression,
118-
char::<&str, Error<&str>>(')'),
118+
char::<&str, Error<&str>>(RIGHT_PAREN),
119119
),
120120
))(input)
121121
}
@@ -193,30 +193,30 @@ pub fn parse_actual_arguments(input: &str) -> IResult<&str, Vec<Expression>> {
193193
map(
194194
tuple((
195195
multispace0,
196-
char::<&str, Error<&str>>('('),
196+
char::<&str, Error<&str>>(LEFT_PAREN),
197197
separated_list0(
198-
tuple((multispace0, char::<&str, Error<&str>>(','), multispace0)),
198+
tuple((multispace0, char::<&str, Error<&str>>(COMMA_CHAR), multispace0)),
199199
parse_expression,
200200
),
201201
multispace0,
202-
char::<&str, Error<&str>>(')'),
202+
char::<&str, Error<&str>>(RIGHT_PAREN),
203203
)),
204204
|(_, _, args, _, _)| args,
205205
)(input)
206206
}
207207

208208
fn parse_list(input: &str) -> IResult<&str, Expression> {
209209
let (input, _) = multispace0(input)?;
210-
let (input, _) = char('[')(input)?;
210+
let (input, _) = char(LEFT_BRACKET)(input)?;
211211
let (input, _) = multispace0(input)?;
212212

213213
let (input, elements) = separated_list0(
214-
delimited(multispace0, char(','), multispace0),
214+
delimited(multispace0, char(COMMA_CHAR), multispace0),
215215
parse_expression,
216216
)(input)?;
217217

218218
let (input, _) = multispace0(input)?;
219-
let (input, _) = char(']')(input)?;
219+
let (input, _) = char(RIGHT_BRACKET)(input)?;
220220
let (input, _) = multispace0(input)?;
221221

222222
Ok((input, Expression::ListValue(elements)))

src/parser/parser_stmt.rs

Lines changed: 35 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,25 @@ use nom::{
22
branch::alt,
33
bytes::complete::tag,
44
character::complete::{char, multispace0, multispace1},
5-
combinator::{map, map_res, opt, value, verify},
5+
combinator::{map, opt},
66
error::Error,
7-
multi::{fold_many0, separated_list0},
8-
sequence::{delimited, pair, preceded, tuple},
7+
multi::separated_list0,
8+
sequence::{delimited, preceded, tuple},
99
IResult,
1010
};
1111

12-
use crate::ir::ast::{Expression, FormalArgument, Function, Statement, Type};
13-
use crate::parser::parser_common::{identifier, keyword};
14-
use crate::parser::parser_expr::{parse_actual_arguments, parse_expression};
12+
use crate::ir::ast::{FormalArgument, Function, Statement};
13+
use crate::parser::parser_common::{
14+
identifier, keyword,
15+
// Statement keyword constants
16+
IF_KEYWORD, ELSE_KEYWORD, WHILE_KEYWORD, FOR_KEYWORD, IN_KEYWORD,
17+
ASSERT_KEYWORD, DEF_KEYWORD, END_KEYWORD,
18+
// Operator and symbol constants
19+
FUNCTION_ARROW,
20+
// Character constants
21+
LEFT_PAREN, RIGHT_PAREN, COLON_CHAR, SEMICOLON_CHAR, COMMA_CHAR, EQUALS_CHAR,
22+
};
23+
use crate::parser::parser_expr::parse_expression;
1524
use crate::parser::parser_type::parse_type;
1625

1726
pub fn parse_statement(input: &str) -> IResult<&str, Statement> {
@@ -29,7 +38,7 @@ fn parse_assignment_statement(input: &str) -> IResult<&str, Statement> {
2938
map(
3039
tuple((
3140
delimited(multispace0, identifier, multispace0),
32-
char::<&str, Error<&str>>('='),
41+
char::<&str, Error<&str>>(EQUALS_CHAR),
3342
delimited(multispace0, parse_expression, multispace0),
3443
)),
3544
|(var, _, expr)| Statement::Assignment(var.to_string(), Box::new(expr)),
@@ -39,10 +48,10 @@ fn parse_assignment_statement(input: &str) -> IResult<&str, Statement> {
3948
fn parse_if_else_statement(input: &str) -> IResult<&str, Statement> {
4049
map(
4150
tuple((
42-
keyword("if"),
51+
keyword(IF_KEYWORD),
4352
preceded(multispace1, parse_expression),
4453
parse_block,
45-
opt(preceded(tuple((multispace0, keyword("else"))), parse_block)),
54+
opt(preceded(tuple((multispace0, keyword(ELSE_KEYWORD))), parse_block)),
4655
)),
4756
|(_, cond, then_block, else_block)| {
4857
Statement::IfThenElse(
@@ -57,7 +66,7 @@ fn parse_if_else_statement(input: &str) -> IResult<&str, Statement> {
5766
fn parse_while_statement(input: &str) -> IResult<&str, Statement> {
5867
map(
5968
tuple((
60-
keyword("while"),
69+
keyword(WHILE_KEYWORD),
6170
preceded(multispace1, parse_expression),
6271
parse_block,
6372
)),
@@ -68,9 +77,9 @@ fn parse_while_statement(input: &str) -> IResult<&str, Statement> {
6877
fn parse_for_statement(input: &str) -> IResult<&str, Statement> {
6978
map(
7079
tuple((
71-
keyword("for"),
80+
keyword(FOR_KEYWORD),
7281
preceded(multispace1, identifier),
73-
preceded(multispace0, keyword("in")),
82+
preceded(multispace0, keyword(IN_KEYWORD)),
7483
preceded(multispace1, parse_expression),
7584
parse_block,
7685
)),
@@ -81,14 +90,14 @@ fn parse_for_statement(input: &str) -> IResult<&str, Statement> {
8190
fn parse_assert_statement(input: &str) -> IResult<&str, Statement> {
8291
map(
8392
tuple((
84-
keyword("assert"),
93+
keyword(ASSERT_KEYWORD),
8594
delimited(
86-
char::<&str, Error<&str>>('('),
95+
char::<&str, Error<&str>>(LEFT_PAREN),
8796
separated_list0(
88-
tuple((multispace0, char::<&str, Error<&str>>(','), multispace0)),
97+
tuple((multispace0, char::<&str, Error<&str>>(COMMA_CHAR), multispace0)),
8998
parse_expression,
9099
),
91-
char::<&str, Error<&str>>(')'),
100+
char::<&str, Error<&str>>(RIGHT_PAREN),
92101
),
93102
)),
94103
|(_, args)| {
@@ -103,17 +112,17 @@ fn parse_assert_statement(input: &str) -> IResult<&str, Statement> {
103112
fn parse_function_definition_statement(input: &str) -> IResult<&str, Statement> {
104113
map(
105114
tuple((
106-
keyword("def"),
115+
keyword(DEF_KEYWORD),
107116
preceded(multispace1, identifier),
108117
delimited(
109-
char::<&str, Error<&str>>('('),
118+
char::<&str, Error<&str>>(LEFT_PAREN),
110119
separated_list0(
111-
tuple((multispace0, char::<&str, Error<&str>>(','), multispace0)),
120+
tuple((multispace0, char::<&str, Error<&str>>(COMMA_CHAR), multispace0)),
112121
parse_formal_argument,
113122
),
114-
char::<&str, Error<&str>>(')'),
123+
char::<&str, Error<&str>>(RIGHT_PAREN),
115124
),
116-
preceded(multispace0, tag("->")),
125+
preceded(multispace0, tag(FUNCTION_ARROW)),
117126
preceded(multispace0, parse_type),
118127
parse_block,
119128
)),
@@ -131,14 +140,14 @@ fn parse_function_definition_statement(input: &str) -> IResult<&str, Statement>
131140
fn parse_block(input: &str) -> IResult<&str, Statement> {
132141
map(
133142
tuple((
134-
char::<&str, Error<&str>>(':'),
143+
char::<&str, Error<&str>>(COLON_CHAR),
135144
multispace0,
136145
separated_list0(
137-
delimited(multispace0, char::<&str, Error<&str>>(';'), multispace0),
146+
delimited(multispace0, char::<&str, Error<&str>>(SEMICOLON_CHAR), multispace0),
138147
parse_statement,
139148
),
140-
opt(preceded(multispace0, char::<&str, Error<&str>>(';'))),
141-
delimited(multispace0, keyword("end"), multispace0),
149+
opt(preceded(multispace0, char::<&str, Error<&str>>(SEMICOLON_CHAR))),
150+
delimited(multispace0, keyword(END_KEYWORD), multispace0),
142151
)),
143152
|(_, _, stmts, _, _)| Statement::Block(stmts),
144153
)(input)
@@ -148,7 +157,7 @@ fn parse_formal_argument(input: &str) -> IResult<&str, FormalArgument> {
148157
map(
149158
tuple((
150159
preceded(multispace0, identifier),
151-
preceded(multispace0, char::<&str, Error<&str>>(':')),
160+
preceded(multispace0, char::<&str, Error<&str>>(COLON_CHAR)),
152161
preceded(multispace0, parse_type),
153162
)),
154163
|(name, _, t)| FormalArgument::new(name.to_string(), t),

0 commit comments

Comments
 (0)