Skip to content
This repository was archived by the owner on Jul 24, 2024. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,18 @@ jobs:
with:
command: test
args: -q

fmt:
name: Format
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: actions-rs/toolchain@v1
with:
toolchain: stable
override: true
- run: rustup component add rustfmt
- uses: actions-rs/cargo@v1
with:
command: fmt
args: --all -- --check
6 changes: 3 additions & 3 deletions trunk_lexer/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
mod token;
mod lexer;
mod token;

pub use token::{Token, TokenKind, Span, OpenTagKind};
pub use lexer::{Lexer, LexerError};
pub use lexer::{Lexer, LexerError};
pub use token::{OpenTagKind, Span, Token, TokenKind};
279 changes: 143 additions & 136 deletions trunk_lexer/src/token.rs
Original file line number Diff line number Diff line change
Expand Up @@ -153,145 +153,152 @@ pub struct Token {

impl Default for Token {
fn default() -> Self {
Self { kind: TokenKind::Eof, span: (0, 0) }
Self {
kind: TokenKind::Eof,
span: (0, 0),
}
}
}

impl Display for TokenKind {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{}", match self {
Self::AsteriskEqual => "*=",
Self::ObjectCast => "(object)",
Self::Abstract => "abstract",
Self::Ampersand => "&",
Self::And => "&&",
Self::AndEqual => "&=",
Self::Arrow => "->",
Self::NullsafeArrow => "?->",
Self::Array => "array",
Self::ArrayCast => "(array)",
Self::As => "as",
Self::Asterisk => "*",
Self::Attribute => "#[",
Self::Bang => "!",
Self::BoolCast => "(bool)",
Self::BooleanAnd => "&&",
Self::BooleanOr => "||",
Self::Break => "break",
Self::Callable => "callable",
Self::Caret => "^",
Self::Case => "case",
Self::Catch => "catch",
Self::Class => "class",
Self::ClassConstant => "__CLASS__",
Self::Clone => "clone",
Self::CloseTag => "?>",
Self::Coalesce => "??",
Self::CoalesceEqual => "??=",
Self::Colon => ":",
Self::Comma => ",",
Self::Comment(comment) => &comment[..],
Self::ConcatEqual => ".=",
Self::Const => "const",
Self::ConstantString(comment) => &comment[..],
Self::Continue => "continue",
Self::IntCast => "(int)",
Self::CurlyOpen => "{$",
Self::Declare => "declare",
Self::Decrement => "--",
Self::Default => "default",
Self::DirConstant => "__DIR__",
Self::DivEqual => "/=",
Self::Do => "do",
Self::DocComment(comment) => &comment[..],
Self::DocOpen(doc_open) => &doc_open[..],
Self::Dot => ".",
Self::DotEquals => ".=",
Self::DoubleArrow => "=>",
Self::DoubleCast => "(float)",
Self::DoubleColon => "::",
Self::DoubleEquals => "==",
Self::Echo => "echo",
Self::Ellipsis => "...",
Self::Else => "else",
Self::ElseIf => "elseif",
Self::Empty => "empty",
Self::EndDeclare => "enddeclare",
Self::EndFor => "endfor",
Self::EndForeach => "endforeach",
Self::EndIf => "endif",
Self::EndSwitch => "endswitch",
Self::EndWhile => "endwhile",
Self::Enum => "enum",
Self::Eof => "",
Self::Equals => "=",
Self::Extends => "extends",
Self::False => "false",
Self::Final => "final",
Self::Finally => "finally",
Self::Float(_) => "float",
Self::Fn => "fn",
Self::For => "for",
Self::FullyQualifiedIdentifier(id) => &id[..],
Self::Function => "function",
Self::GreaterThan => ">",
Self::GreaterThanEquals => ">=",
Self::Identifier(id) => &id[..],
Self::If => "if",
Self::Implements => "implements",
Self::Increment => "++",
Self::InlineHtml(_) => "InlineHtml",
Self::Int(_) => "int",
Self::LeftBrace => "{",
Self::LeftBracket => "[",
Self::LeftParen => "(",
Self::LeftShift => "<<",
Self::LessThan => "<",
Self::LessThanEquals => "<=",
Self::Match => "match",
Self::Minus => "-",
Self::MinusEquals => "-=",
Self::Namespace => "namespace",
Self::NamespaceSeparator => "\\",
Self::New => "new",
Self::Null => "null",
Self::OpenTag(kind) => match kind {
OpenTagKind::Full => "<?php",
},
Self::Percent => "%",
Self::Pipe => "|",
Self::Plus => "+",
Self::PlusEquals => "+=",
Self::Pow => "**",
Self::Private => "private",
Self::Protected => "protected",
Self::Public => "public",
Self::QualifiedIdentifier(id) => &id[..],
Self::Question => "?",
Self::QuestionColon => "?:",
Self::Require => "require",
Self::RequireOnce => "require_once",
Self::Return => "return",
Self::RightBrace => "}",
Self::RightBracket => "]",
Self::RightParen => ")",
Self::SemiColon => ";",
Self::Slash => "/",
Self::SlashEquals => "/=",
Self::Static => "static",
Self::StringCast => "(string)",
Self::Switch => "switch",
Self::Throw => "throw",
Self::Trait => "trait",
Self::TripleEquals => "===",
Self::True => "true",
Self::Try => "try",
Self::Use => "use",
Self::Var => "var",
Self::Variable(var) => &var[..],
Self::Yield => "yield",
Self::While => "while",
_ => todo!("format token: {:?}", self)
})
write!(
f,
"{}",
match self {
Self::AsteriskEqual => "*=",
Self::ObjectCast => "(object)",
Self::Abstract => "abstract",
Self::Ampersand => "&",
Self::And => "&&",
Self::AndEqual => "&=",
Self::Arrow => "->",
Self::NullsafeArrow => "?->",
Self::Array => "array",
Self::ArrayCast => "(array)",
Self::As => "as",
Self::Asterisk => "*",
Self::Attribute => "#[",
Self::Bang => "!",
Self::BoolCast => "(bool)",
Self::BooleanAnd => "&&",
Self::BooleanOr => "||",
Self::Break => "break",
Self::Callable => "callable",
Self::Caret => "^",
Self::Case => "case",
Self::Catch => "catch",
Self::Class => "class",
Self::ClassConstant => "__CLASS__",
Self::Clone => "clone",
Self::CloseTag => "?>",
Self::Coalesce => "??",
Self::CoalesceEqual => "??=",
Self::Colon => ":",
Self::Comma => ",",
Self::Comment(comment) => &comment[..],
Self::ConcatEqual => ".=",
Self::Const => "const",
Self::ConstantString(comment) => &comment[..],
Self::Continue => "continue",
Self::IntCast => "(int)",
Self::CurlyOpen => "{$",
Self::Declare => "declare",
Self::Decrement => "--",
Self::Default => "default",
Self::DirConstant => "__DIR__",
Self::DivEqual => "/=",
Self::Do => "do",
Self::DocComment(comment) => &comment[..],
Self::DocOpen(doc_open) => &doc_open[..],
Self::Dot => ".",
Self::DotEquals => ".=",
Self::DoubleArrow => "=>",
Self::DoubleCast => "(float)",
Self::DoubleColon => "::",
Self::DoubleEquals => "==",
Self::Echo => "echo",
Self::Ellipsis => "...",
Self::Else => "else",
Self::ElseIf => "elseif",
Self::Empty => "empty",
Self::EndDeclare => "enddeclare",
Self::EndFor => "endfor",
Self::EndForeach => "endforeach",
Self::EndIf => "endif",
Self::EndSwitch => "endswitch",
Self::EndWhile => "endwhile",
Self::Enum => "enum",
Self::Eof => "",
Self::Equals => "=",
Self::Extends => "extends",
Self::False => "false",
Self::Final => "final",
Self::Finally => "finally",
Self::Float(_) => "float",
Self::Fn => "fn",
Self::For => "for",
Self::FullyQualifiedIdentifier(id) => &id[..],
Self::Function => "function",
Self::GreaterThan => ">",
Self::GreaterThanEquals => ">=",
Self::Identifier(id) => &id[..],
Self::If => "if",
Self::Implements => "implements",
Self::Increment => "++",
Self::InlineHtml(_) => "InlineHtml",
Self::Int(_) => "int",
Self::LeftBrace => "{",
Self::LeftBracket => "[",
Self::LeftParen => "(",
Self::LeftShift => "<<",
Self::LessThan => "<",
Self::LessThanEquals => "<=",
Self::Match => "match",
Self::Minus => "-",
Self::MinusEquals => "-=",
Self::Namespace => "namespace",
Self::NamespaceSeparator => "\\",
Self::New => "new",
Self::Null => "null",
Self::OpenTag(kind) => match kind {
OpenTagKind::Full => "<?php",
},
Self::Percent => "%",
Self::Pipe => "|",
Self::Plus => "+",
Self::PlusEquals => "+=",
Self::Pow => "**",
Self::Private => "private",
Self::Protected => "protected",
Self::Public => "public",
Self::QualifiedIdentifier(id) => &id[..],
Self::Question => "?",
Self::QuestionColon => "?:",
Self::Require => "require",
Self::RequireOnce => "require_once",
Self::Return => "return",
Self::RightBrace => "}",
Self::RightBracket => "]",
Self::RightParen => ")",
Self::SemiColon => ";",
Self::Slash => "/",
Self::SlashEquals => "/=",
Self::Static => "static",
Self::StringCast => "(string)",
Self::Switch => "switch",
Self::Throw => "throw",
Self::Trait => "trait",
Self::TripleEquals => "===",
Self::True => "true",
Self::Try => "try",
Self::Use => "use",
Self::Var => "var",
Self::Variable(var) => &var[..],
Self::Yield => "yield",
Self::While => "while",
_ => todo!("format token: {:?}", self),
}
)
}
}
}
Loading