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
feature: recognise __COMPILER_HALT_OFFSET__ as a magic constant
  • Loading branch information
ryangjchandler committed Dec 25, 2022
commit 2db66342856bfa4879d9f90f9a64d526013798f7
1 change: 1 addition & 0 deletions src/lexer/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1840,6 +1840,7 @@ fn identifier_to_keyword(ident: &[u8]) -> Option<TokenKind> {
b"__method__" => TokenKind::MethodConstant,
b"__trait__" => TokenKind::TraitConstant,
b"__namespace__" => TokenKind::NamespaceConstant,
b"__compiler_halt_offset__" => TokenKind::CompilerHaltOffsetConstant,
b"while" => TokenKind::While,
b"insteadof" => TokenKind::Insteadof,
b"list" => TokenKind::List,
Expand Down
2 changes: 2 additions & 0 deletions src/lexer/token.rs
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,7 @@ pub enum TokenKind {
Namespace,
NamespaceSeparator,
NamespaceConstant,
CompilerHaltOffsetConstant,
New,
Null,
ObjectCast,
Expand Down Expand Up @@ -288,6 +289,7 @@ impl Display for Token {
impl Display for TokenKind {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
let s = match self {
Self::CompilerHaltOffsetConstant => "__COMPILER_HALT_OFFSET__",
Self::Die => "die",
Self::Self_ => "self",
Self::Parent => "parent",
Expand Down
1 change: 1 addition & 0 deletions src/parser/ast/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -516,6 +516,7 @@ pub enum MagicConstant {
Method(Span),
Namespace(Span),
Trait(Span),
CompilerHaltOffset(Span),
}

#[derive(Debug, PartialEq, Eq, Clone, Deserialize, Serialize, JsonSchema)]
Expand Down
10 changes: 9 additions & 1 deletion src/parser/expressions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -989,14 +989,22 @@ expressions! {
Ok(Expression::MagicConstant(MagicConstant::Namespace(span)))
})

#[before(include), current(TokenKind::TraitConstant)]
#[before(compiler_halt_offset_magic_constant), current(TokenKind::TraitConstant)]
trait_magic_constant({
let span = state.stream.current().span;
state.stream.next();

Ok(Expression::MagicConstant(MagicConstant::Trait(span)))
})

#[before(include), current(TokenKind::CompilerHaltOffsetConstant)]
compiler_halt_offset_magic_constant({
let span = state.stream.current().span;
state.stream.next();

Ok(Expression::MagicConstant(MagicConstant::CompilerHaltOffset(span)))
})

#[before(cast_prefix), current(TokenKind::Include | TokenKind::IncludeOnce | TokenKind::Require | TokenKind::RequireOnce)]
include({
let current = state.stream.current();
Expand Down
1 change: 1 addition & 0 deletions src/parser/internal/identifiers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -493,6 +493,7 @@ pub fn is_reserved_identifier(kind: &TokenKind) -> bool {
| TokenKind::DirConstant
| TokenKind::NamespaceConstant
| TokenKind::HaltCompiler
| TokenKind::CompilerHaltOffsetConstant
| TokenKind::Fn
| TokenKind::Match
)
Expand Down
27 changes: 27 additions & 0 deletions tests/fixtures/0329-compiler-halt-offset/ast.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
[
FullOpeningTag(
Span {
line: 1,
column: 1,
position: 0,
},
),
Expression {
expression: MagicConstant(
CompilerHaltOffset(
Span {
line: 3,
column: 1,
position: 7,
},
),
),
ending: Semicolon(
Span {
line: 3,
column: 25,
position: 31,
},
),
},
]
3 changes: 3 additions & 0 deletions tests/fixtures/0329-compiler-halt-offset/code.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<?php

__COMPILER_HALT_OFFSET__;