Skip to main content
Drupal API
User account menu
  • Log in

Breadcrumb

  1. Drupal Core 11.1.x
  2. Scanner.php

function Scanner::scanKeywordOrIdentifier

Keywords and identifiers scanning method

Return value

Token|null

1 call to Scanner::scanKeywordOrIdentifier()
Scanner::getToken in vendor/mck89/peast/lib/Peast/Syntax/Scanner.php
Returns the current token

File

vendor/mck89/peast/lib/Peast/Syntax/Scanner.php, line 1634

Class

Scanner
Base class for scanners.

Namespace

Peast\Syntax

Code

protected function scanKeywordOrIdentifier() {
    
    //Check private identifier start character
    if ($private = $this->features->privateMethodsAndFields && $this->charAt() === "#") {
        $this->index++;
        $this->column++;
    }
    
    //Consume the maximum number of characters that are unicode escape
    
    //sequences or valid identifier starts (only the first character) or
    
    //parts
    $buffer = "";
    $start = true;
    while (($char = $this->charAt()) !== null) {
        if ($char >= "a" && $char <= "z" || $char >= "A" && $char <= "Z" || $char === "_" || $char === "\$" || !$start && $char >= "0" && $char <= "9" || $this->isIdentifierChar($char, $start)) {
            $buffer .= $char;
            $this->index++;
            $this->column++;
        }
        elseif ($char === "\\" && ($seq = $this->consumeUnicodeEscapeSequence())) {
            
            //Verify that it's a valid character
            if (!$this->isIdentifierChar($seq[1], $start)) {
                break;
            }
            $buffer .= $seq[0];
        }
        else {
            break;
        }
        $start = false;
    }
    
    //Identify token type
    if ($buffer === "") {
        
        //Unconsume the hash if nothing was found after that
        if ($private) {
            $this->index--;
            $this->column--;
        }
        return null;
    }
    elseif ($private) {
        $type = Token::TYPE_PRIVATE_IDENTIFIER;
        $buffer = "#" . $buffer;
    }
    elseif ($buffer === "null") {
        $type = Token::TYPE_NULL_LITERAL;
    }
    elseif ($buffer === "true" || $buffer === "false") {
        $type = Token::TYPE_BOOLEAN_LITERAL;
    }
    elseif (in_array($buffer, $this->keywords) || in_array($buffer, $this->strictModeKeywords)) {
        $type = Token::TYPE_KEYWORD;
    }
    else {
        $type = Token::TYPE_IDENTIFIER;
    }
    return new Token($type, $buffer);
}

API Navigation

  • Drupal Core 11.1.x
  • Topics
  • Classes
  • Functions
  • Constants
  • Globals
  • Files
  • Namespaces
  • Deprecated
  • Services
RSS feed
Powered by Drupal