Skip to content

Commit 368a18f

Browse files
committed
regex template parser
1 parent 4b43727 commit 368a18f

5 files changed

Lines changed: 258 additions & 197 deletions

File tree

htmlsnob/src/ast.rs

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -96,19 +96,19 @@ pub struct CloseTag {
9696

9797
#[derive(Debug, PartialEq, Clone)]
9898
pub enum Construct {
99-
Statement,
100-
Expression,
101-
If,
102-
Else,
103-
EndIf,
104-
Loop,
105-
EndLoop,
106-
Switch,
107-
Case,
108-
EndSwitch,
109-
Block,
110-
EndBlock,
111-
Comment,
99+
Statement, // TODO: Change to:
100+
Expression, // Statement
101+
If, // Open
102+
Else, // Else
103+
EndIf, // Close
104+
Loop, // Open
105+
EndLoop, // Close
106+
Switch, // Open
107+
Case, // Open
108+
EndSwitch, // Close
109+
Block, // Open
110+
EndBlock, // Close
111+
Comment, // Comment
112112
}
113113

114114
#[derive(Debug, PartialEq, Clone)]

htmlsnob/src/parser.rs

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -564,7 +564,7 @@ impl Parser {
564564

565565
TemplateLanguage::constructs(&self.template_language)
566566
.into_iter()
567-
.any(|c| self.peek_str(c.0))
567+
.any(|construct| self.peek_regex(&construct.0))
568568
}
569569

570570
fn parse_template_expression(&mut self) -> TemplateExpression {
@@ -573,7 +573,7 @@ impl Parser {
573573

574574
let construct = TemplateLanguage::constructs(&self.template_language)
575575
.into_iter()
576-
.find(|c| self.peek_str(c.0))
576+
.find(|construct| self.peek_regex(&construct.0))
577577
.unwrap_or_else(|| panic!("No matching construct found for input"));
578578

579579
let mut quote = None;
@@ -602,8 +602,8 @@ impl Parser {
602602
}
603603
}
604604

605-
if quote.is_none() && self.peek_str(construct.2) {
606-
self.consume_str(construct.2);
605+
if quote.is_none() && self.peek_regex(&construct.2) {
606+
self.consume_regex(&construct.2);
607607
break;
608608
}
609609

@@ -657,6 +657,27 @@ impl Parser {
657657
true
658658
}
659659

660+
fn peek_regex(&self, regex: &regex::Regex) -> bool {
661+
let remaining_input: String = self.input[self.cursor..].iter().collect();
662+
regex.is_match(&remaining_input)
663+
}
664+
665+
fn consume_regex(&mut self, regex: &regex::Regex) {
666+
let remaining_input: String = self.input[self.cursor..].iter().collect();
667+
if let Some(regex_match) = regex.find(&remaining_input) {
668+
assert_eq!(
669+
regex_match.start(),
670+
0,
671+
"Regex did not match at the current position"
672+
);
673+
for _ in 0..regex_match.end() {
674+
self.advance();
675+
}
676+
} else {
677+
panic!("Regex did not match the input");
678+
}
679+
}
680+
660681
fn peek_any(&self, list: &[&str]) -> bool {
661682
for &s in list {
662683
if self.peek_str(s) {

0 commit comments

Comments
 (0)