Skip to content

Commit

Permalink
handle parse_token specially
Browse files Browse the repository at this point in the history
  • Loading branch information
ratmice committed Dec 23, 2023
1 parent 20351df commit a9b432a
Showing 1 changed file with 51 additions and 18 deletions.
69 changes: 51 additions & 18 deletions cfgrammar/src/lib/yacc/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -353,7 +353,12 @@ impl YaccParser {
if let Some(j) = self.lookahead_is("%token", i) {
i = self.parse_ws(j, false, errs)?;
while i < self.src.len() && self.lookahead_is("%", i).is_none() {
let (j, n, span) = self.parse_token(i, errs)?;
let result = self.parse_token(i);
if let Err(e) = result {
errs.send(e)?;
return Err(YaccGrammarConstructionFailure::ConstructionFailure);
}
let (j, n, span) = result.unwrap();
if self.ast.tokens.insert(n) {
self.ast.spans.push(span);
}
Expand Down Expand Up @@ -400,7 +405,12 @@ impl YaccParser {
}
if let Some(j) = self.lookahead_is("%epp", i) {
i = self.parse_ws(j, false, errs)?;
let (j, n, _) = self.parse_token(i, errs)?;
let result = self.parse_token(i);
if let Err(e) = result {
errs.send(e)?;
return Err(YaccGrammarConstructionFailure::ConstructionFailure);
}
let (j, n, _) = result.unwrap();
let span = Span::new(i, j);
i = self.parse_ws(j, false, errs)?;
let (j, v) = self.parse_string(i, errs)?;
Expand Down Expand Up @@ -446,7 +456,7 @@ impl YaccParser {
.push(Symbol::Rule(n, Span::new(i, j)));
j
}
Err(_) => match self.parse_token(i, errs) {
Err(_) => match self.parse_token(i) {
Ok((j, n, span)) => {
self.ast.expect_unused.push(Symbol::Token(n, span));
j
Expand Down Expand Up @@ -483,7 +493,12 @@ impl YaccParser {
self.ast.avoid_insert = Some(HashMap::new());
}
while j < self.src.len() && self.num_newlines == num_newlines {
let (j, n, span) = self.parse_token(i, errs)?;
let result = self.parse_token(i);
if let Err(e) = result {
errs.send(e)?;
return Err(YaccGrammarConstructionFailure::ConstructionFailure);
}
let (j, n, span) = result.unwrap();
if self.ast.tokens.insert(n.clone()) {
self.ast.spans.push(span);
}
Expand Down Expand Up @@ -526,7 +541,12 @@ impl YaccParser {
self.ast.implicit_tokens = Some(HashMap::new());
}
while j < self.src.len() && self.num_newlines == num_newlines {
let (j, n, span) = self.parse_token(i, errs)?;
let result = self.parse_token(i);
if let Err(e) = result {
errs.send(e)?;
return Err(YaccGrammarConstructionFailure::ConstructionFailure);
}
let (j, n, span) = result.unwrap();
if self.ast.tokens.insert(n.clone()) {
self.ast.spans.push(span);
}
Expand Down Expand Up @@ -567,7 +587,12 @@ impl YaccParser {
i = self.parse_ws(k, false, errs)?;
let num_newlines = self.num_newlines;
while i < self.src.len() && num_newlines == self.num_newlines {
let (j, n, span) = self.parse_token(i, errs)?;
let result = self.parse_token(i);
if let Err(e) = result {
errs.send(e)?;
return Err(YaccGrammarConstructionFailure::ConstructionFailure);
}
let (j, n, span) = result.unwrap();
match self.ast.precs.entry(n) {
Entry::Occupied(orig) => {
let (_, orig_span) = orig.get();
Expand Down Expand Up @@ -677,15 +702,25 @@ impl YaccParser {
}

if self.lookahead_is("\"", i).is_some() || self.lookahead_is("'", i).is_some() {
let (j, sym, span) = self.parse_token(i, errs)?;
let result = self.parse_token(i);
if let Err(e) = result {
errs.send(e)?;
return Err(YaccGrammarConstructionFailure::ConstructionFailure);
}
let (j, sym, span) = result.unwrap();
i = self.parse_ws(j, true, errs)?;
if self.ast.tokens.insert(sym.clone()) {
self.ast.spans.push(span);
}
syms.push(Symbol::Token(sym, span));
} else if let Some(j) = self.lookahead_is("%prec", i) {
i = self.parse_ws(j, true, errs)?;
let (k, sym, _) = self.parse_token(i, errs)?;
let result = self.parse_token(i);
if let Err(e) = result {
errs.send(e)?;
return Err(YaccGrammarConstructionFailure::ConstructionFailure);
}
let (k, sym, _) = result.unwrap();
if self.ast.tokens.contains(&sym) {
prec = Some(sym);
} else {
Expand All @@ -712,7 +747,12 @@ impl YaccParser {
}
i = j;
} else {
let (j, sym, span) = self.parse_token(i, errs)?;
let result = self.parse_token(i);
if let Err(e) = result {
errs.send(e)?;
return Err(YaccGrammarConstructionFailure::ConstructionFailure);
}
let (j, sym, span) = result.unwrap();
if self.ast.tokens.contains(&sym) {
syms.push(Symbol::Token(sym, span));
} else {
Expand All @@ -736,11 +776,7 @@ impl YaccParser {
}
}

fn parse_token(
&self,
i: usize,
errs: &mut ErrorSender,
) -> Result<(usize, String, Span), YaccGrammarConstructionFailure> {
fn parse_token(&self, i: usize) -> Result<(usize, String, Span), YaccGrammarError> {
match RE_TOKEN.find(&self.src[i..]) {
Some(m) => {
assert!(m.start() == 0 && m.end() > 0);
Expand All @@ -762,10 +798,7 @@ impl YaccParser {
)),
}
}
None => {
errs.send(self.mk_error(YaccGrammarErrorKind::IllegalString, i))?;
Err(YaccGrammarConstructionFailure::ConstructionFailure)
}
None => Err(self.mk_error(YaccGrammarErrorKind::IllegalString, i)),
}
}

Expand Down

0 comments on commit a9b432a

Please sign in to comment.