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

Breadcrumb

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

function Scanner::getToken

Returns the current token

Parameters

bool $skipEOFChecks True to skip end of file checks: even if the end is reached

Return value

Token|null

7 calls to Scanner::getToken()
Scanner::consume in vendor/mck89/peast/lib/Peast/Syntax/Scanner.php
Checks if the given string is matched, if so it consumes the token
Scanner::consumeOneOf in vendor/mck89/peast/lib/Peast/Syntax/Scanner.php
Checks if one of the given strings is matched, if so it consumes the token
Scanner::getNextToken in vendor/mck89/peast/lib/Peast/Syntax/Scanner.php
Returns the next token
Scanner::getState in vendor/mck89/peast/lib/Peast/Syntax/Scanner.php
Returns the current scanner state
Scanner::isBefore in vendor/mck89/peast/lib/Peast/Syntax/Scanner.php
Checks if one of the given strings follows the current scan position

... See full list

File

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

Class

Scanner
Base class for scanners.

Namespace

Peast\Syntax

Code

public function getToken($skipEOFChecks = false) {
    
    //The current token is returned until consumed
    if ($this->currentToken) {
        return $this->currentToken;
    }
    $comments = $this->skipWhitespacesAndComments();
    
    //Emit the TokenCreated event for all the comments found
    if ($comments) {
        foreach ($comments as $comment) {
            $this->eventsEmitter && $this->eventsEmitter
                ->fire("TokenCreated", array(
                $comment,
            ));
        }
    }
    
    //When the end of the source is reached
    if ($this->index >= $this->length) {
        
        //Check if there are open brackets
        if (!$skipEOFChecks) {
            foreach ($this->openBrackets as $bracket => $num) {
                if ($num) {
                    $this->error("Unclosed {$bracket}");
                }
            }
            
            //Check if there are open templates
            if (count($this->openTemplates)) {
                $this->error("Unterminated template");
            }
        }
        
        //Register comments and consume them
        if ($this->comments && $comments) {
            $this->commentsForCurrentToken($comments);
        }
        
        //Emit the EndReached event when at the end of the source
        $this->eventsEmitter && $this->eventsEmitter
            ->fire("EndReached");
        return null;
    }
    $startPosition = $this->getPosition(true);
    $origException = null;
    try {
        
        //Try to match a token
        if ($this->jsx && ($token = $this->scanJSXIdentifier()) || ($token = $this->scanTemplate()) || ($token = $this->scanNumber()) || $this->jsx && ($token = $this->scanJSXPunctuator()) || ($token = $this->scanPunctuator()) || ($token = $this->scanKeywordOrIdentifier()) || $this->jsx && ($token = $this->scanJSXString()) || ($token = $this->scanString())) {
            
            //Set the token start and end positions
            $token->location->start = $startPosition;
            $token->location->end = $this->getPosition(true);
            $this->currentToken = $token;
            
            //Register comments if required
            if ($this->comments && $comments) {
                $this->commentsForCurrentToken($comments);
            }
            
            //Emit the TokenCreated event for the token just created
            $this->eventsEmitter && $this->eventsEmitter
                ->fire("TokenCreated", array(
                $this->currentToken,
            ));
            return $this->currentToken;
        }
    } catch (Exception $e) {
        $origException = $e;
    }
    
    //If last token was "/" do not throw an error if the token has not be
    
    //recognized since it can be the first character in a regexp and it will
    
    //be consumed when the current token will be reconsumed as a regexp
    if ($this->isAfterSlash($startPosition)) {
        $this->setScanPosition($startPosition);
        return null;
    }
    
    //No valid token found. If there was a scan error, throw the same
    
    //exception again, otherwise throw a new error
    if ($origException) {
        throw $origException;
    }
    $this->error();
}

API Navigation

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