lexer&parser: better support for close tags

This commit is contained in:
Ryan Chandler 2022-09-12 12:31:01 +01:00
parent 028e2de1d3
commit bdce626104
No known key found for this signature in database
GPG Key ID: F113BCADDB3B0CCA
2 changed files with 28 additions and 1 deletions

View File

@ -134,6 +134,8 @@ impl Lexer {
buffer.push('?');
}
} else {
self.next();
self.col += 1;
buffer.push(char);
@ -203,6 +205,7 @@ impl Lexer {
// This is a close tag, we can enter "Initial" mode again.
if let Some('>') = self.peek {
self.next();
self.next();
self.col += 2;
@ -911,6 +914,18 @@ mod tests {
assert_tokens("<?php ?>", &[open!(), TokenKind::CloseTag]);
}
#[test]
fn close_tag_followed_by_content() {
assert_tokens(
"<?php ?> <html>",
&[
open!(),
TokenKind::CloseTag,
TokenKind::InlineHtml(" <html>".into()),
],
);
}
#[test]
fn inline_html() {
assert_tokens(

View File

@ -5,6 +5,7 @@ use crate::{
},
Block, Case, Catch, Expression, Identifier, MatchArm, Program, Statement, Type,
};
use core::panic;
use std::{fmt::Display, vec::IntoIter};
use trunk_lexer::{Span, Token, TokenKind};
@ -89,7 +90,10 @@ impl Parser {
let mut ast = Program::new();
while self.current.kind != TokenKind::Eof {
if let TokenKind::OpenTag(_) = self.current.kind {
if matches!(
self.current.kind,
TokenKind::OpenTag(_) | TokenKind::CloseTag
) {
self.next();
continue;
}
@ -3088,6 +3092,14 @@ mod tests {
)
}
#[test]
fn close_tag_followed_by_content() {
assert_ast(
"<?php ?> <html>",
&[Statement::InlineHtml(" <html>".into())],
);
}
fn assert_ast(source: &str, expected: &[Statement]) {
let mut lexer = Lexer::new(None);
let tokens = lexer.tokenize(source).unwrap();