Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 29 additions & 2 deletions src/compiling/lexing/lexer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,8 @@ impl Lexer {
};

while let Some(letter) = lex_state.reader.next() {
let region = lex_state.region_handler.get_region().unwrap();

/****************/
/* Set Position */
/****************/
Expand All @@ -171,7 +173,6 @@ impl Lexer {
// If separator mode is set to Manual and the letter is a separator,
// then skip finding a new position
if SeparatorMode::Manual != self.separator_mode || letter != '\n' {
let region = lex_state.region_handler.get_region().unwrap();
// If the region is tokenized, then check if the letter is a separator
if !region.tokenize || !vec![' ', '\t'].contains(&letter) {
lex_state.position = lex_state.reader.get_position();
Expand All @@ -181,7 +182,7 @@ impl Lexer {

// Reaction stores the reaction of the region handler
// Have we just opened or closed some region?
let reaction = if lex_state.is_escaped {
let reaction = if lex_state.is_escaped && (!region.ignore_escaped) {
RegionReaction::Pass
} else {
lex_state.region_handler.handle_region(&lex_state.reader)
Expand Down Expand Up @@ -500,6 +501,32 @@ mod test {
}
assert_eq!(expected, result);
}

#[test]
fn test_lexer_ignored_escaped_regions() {
let symbols = vec![';', '+', '='];
let mut regions = reg![
reg!(module as "Comment" => {
begin: "//",
end: "\n"
}),
reg!(module as "String" => {
begin: "\"",
end: "\""
})
];
regions.interp[0].ignore_escaped = true;
let expected = vec![("// comment \\\n".to_string(), 1, 1), ("\n".to_string(), 1, 13), ("// not same region\n".to_string(), 2, 1), ("\n".to_string(), 2, 19)];
let rules = Rules::new(symbols, vec![], regions);
let lexer = super::Lexer::new(rules);
let mut result = vec![];
// Simulate lexing
let res = lexer.tokenize(&vec!["// comment \\", "// not same region\n"].join("\n"));
for lex in res.unwrap() {
result.push((lex.word, lex.pos.0, lex.pos.1));
}
assert_eq!(expected, result);
}

// Test if comments are tokenized in the string (it should not be)
#[test]
Expand Down
22 changes: 17 additions & 5 deletions src/compiling_rules/region.rs
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,9 @@ pub struct Region {
/// Region can be a reference to some other region
pub references: Option<String>,
/// Determines if region cannot go past the new line character
pub singleline: bool
pub singleline: bool,
/// Whether to ignore escaped characters for this region only
pub ignore_escaped: bool,
}

impl Region {
Expand All @@ -152,7 +154,8 @@ impl Region {
allow_unclosed_region: false,
global: false,
singleline: false,
references: references.map(|value| String::from(value.as_ref()))
references: references.map(|value| String::from(value.as_ref())),
ignore_escaped: false
}
}

Expand Down Expand Up @@ -211,19 +214,22 @@ mod test {
allow_unclosed_region: false,
singleline: false,
global: false,
references: Some(format!("global"))
references: Some(format!("global")),
ignore_escaped: false,
}],
tokenize: false,
allow_unclosed_region: false,
singleline: false,
global: false,
references: None
references: None,
ignore_escaped: false
}],
tokenize: true,
allow_unclosed_region: true,
global: true,
singleline: false,
references: None
references: None,
ignore_escaped: false
};
let result = reg![
reg!(string as "String Literal" => {
Expand Down Expand Up @@ -256,6 +262,7 @@ mod test {
references: Some(
"global".to_string(),
),
ignore_escaped: false,
});
expected.insert("global".to_string(), Region {
id: "global".to_string(),
Expand All @@ -282,20 +289,23 @@ mod test {
references: Some(
"global".to_string(),
),
ignore_escaped: false,
},
],
tokenize: false,
allow_unclosed_region: false,
global: false,
singleline: false,
references: None,
ignore_escaped: false,
},
],
tokenize: true,
allow_unclosed_region: true,
global: true,
singleline: false,
references: None,
ignore_escaped: false,
});
expected.insert("string".to_string(), Region {
id: "string".to_string(),
Expand All @@ -316,13 +326,15 @@ mod test {
references: Some(
"global".to_string(),
),
ignore_escaped: false,
},
],
tokenize: false,
allow_unclosed_region: false,
global: false,
singleline: false,
references: None,
ignore_escaped: false,
});
let region = reg![
reg!(string as "String Literal" => {
Expand Down