Giter Club home page Giter Club logo

Comments (3)

nikic avatar nikic commented on May 10, 2024

Yup, the parser currently tries to present the AST "as seen by PHP", so it also parses strings and numbers.

One way to retain the original value would be by defining a custom lexer which adds the originalValue as an attribute:

<?php

class KeepOriginalValueLexer extends PHPParser_Lexer // or PHPParser_Lexer_Emulative
{
    public function getNextToken(&$value = null, &$startAttributes = null, &$endAttributes = null) {
        $tokenId = parent::getNextToken($value, $startAttributes, $endAttributes);

        if ($tokenId == PHPParser_Parser::T_CONSTANT_ENCAPSED_STRING
            || $tokenId == PHPParser_Parser::T_LNUMBER
            || $tokenId == PHPParser_Parser::T_DNUMBER
        ) {
            // could also use $startAttributes, doesn't really matter here
            $endAttributes['originalValue'] = $value;
        }

        return $tokenId;
    }
}

This should make the original value available through $node->getAttribute('originalValue').

Would that solution work for you?

from php-parser.

mvriel avatar mvriel commented on May 10, 2024

It will certainly help. The only downside for me is that the regular expression is still used, costing performance

from php-parser.

nikic avatar nikic commented on May 10, 2024

I doubt that the escape sequence parsing really takes much time (at least compared to all the other stuff), but I think that it wouldn't be much of a problem to outsource the scalar value parsing into another class (which is injected into the parser). If you would prefer it that way I'll gladly implement it :)

from php-parser.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.