Giter Site home page Giter Site logo

Comments (10)

pscheit avatar pscheit commented on May 3, 2024
class WebforgeCreateTestForClassCommand(sublime_plugin.WindowCommand):
    def run(self):
        view = self.window.active_view()
        classString = view.substr(view.find("^(?:abstract\s*|final\s*)*(?:class|interface)\s+(\w+)\s+", 0, sublime.IGNORECASE))
        print "searching page"
        print classString
        className = re.match(r"(?:abstract\s*|final\s*)?(?:class|interface)\s+(\w+)\s+", classString, re.I).group(1)

        print "searching ns"
        nsString = view.substr(view.find("namespace\s+(.*?);", 0, sublime.IGNORECASE))
        print nsString
        namespace = re.match(r"namespace\s+(.*?);", nsString).group(1)

        fqn = namespace+"\\"+className

if using with parser try to cut the string of php code before the syntax error appears. Or cut it before the line the syntax error appears. But for rself-made sublime plugins (with some conventions made) regexp would suffer

from php-parser.

deweller avatar deweller commented on May 3, 2024

Thanks. The regex is a good idea for a workaround if I can't solve it with PHP-Parser. I'd like to use PHP-Parser if I can. Any ideas on how to modify or extend PHP-Parser to provide fault-tolerant parsing?

from php-parser.

nikic avatar nikic commented on May 3, 2024

It's possible to make the parser reentrant, but it's not particularly simple (would require some changes to the parsing algorithm and the inclusion of error-recovery information in the grammar). I'll look into this, but can't guarantee it'll be anytime soon.

from php-parser.

deweller avatar deweller commented on May 3, 2024

I'd be willing to contribute the code if you are open to a pull request. It would be a good excuse for me to get my head around how the parsing code works.

from php-parser.

nikic avatar nikic commented on May 3, 2024

Yes, sure :) Just went through the commit history to find the old code for error recovery: b153bfa By now some of the surrounding code may have changed, but it should still work with some small modifications.

The docs for bison error recovery tokens can be found here: http://dinosaur.compilertools.net/bison/bison_9.html I guess one would have to add error rules in some key places like statements (error ';') and blocks (error '}').

The most complicated part is probably figuring out how to best report the errors that occur, e.g. one could introduce Error nodes, though not sure if that's a good idea. Alternatively just put all the errors in some array.

from php-parser.

deweller avatar deweller commented on May 3, 2024

Thanks for the links above. That will help me to get started.

I would expect that the current behavior of throwing an error on a parse failure should stay the same. But perhaps we add an optional parser that reports errors. Perhaps we call it PHPParser_Parser_ErrorReporter. And maybe this version of the parser can recover from errors and report a list of all errors found.

from php-parser.

deweller avatar deweller commented on May 3, 2024

I got as far as restoring the old commented-out test code. Here is my commit:
deweller@b0816a2

It is not obvious to me how $yyerrflag would ever be set to anything but 0. But maybe that is where bison's recovery tokens are required.

from php-parser.

nikic avatar nikic commented on May 3, 2024

I just added an initial implementation in the errorRecovery branch: https://github.com/nikic/PHP-Parser/tree/errorRecovery

A test script:

$parser = new PHPParser_Parser(new PHPParser_Lexer, ['errorCallback' => function() {
    var_dump(func_get_args());
}]);
$nodeDumper = new PHPParser_NodeDumper;

$code = <<<'CODE'
<?php

while (true) {
    doSomething()
    echo 'Hello world';
}
CODE;

echo $nodeDumper->dump($parser->parse($code));

This will output:

array(2) {
  [0]=>
  string(31) "Syntax error, unexpected T_ECHO"
  [1]=>
  array(1) {
    ["startLine"]=>
    int(5)
  }
}
array(
    0: Stmt_While(
        cond: Expr_ConstFetch(
            name: Name(
                parts: array(
                    0: true
                )
            )
        )
        stmts: array(
            0: Stmt_Echo(
                exprs: array(
                    0: Scalar_String(
                        value: Hello world
                    )
                )
            )
        )
    )
)

So you get an error because of the missing semicolon after the call, but the next statement is parsed correctly.

Will need some additional tinkering though, some things that have to be figured out:

  • How to report the errors. Right now it's using an error callback with the error message and the attributes of the token. That's probably not the most convenient way to do it.
  • What to do when the error can not be recovered. Right now I throw an exception, but in the recovery-mode it might make more sense to just return false.
  • How to handle errors that are creates by exceptions thrown during parsing (e.g. invalid modifiers). Right now this will still throw the exception and not be recovered.

from php-parser.

deweller avatar deweller commented on May 3, 2024

This is great.

I went a different route (for now) in solving the problem that required the error recovery.

But I suspect I might need to come back to this later. When I do, I'll test it out.

from php-parser.

nikic avatar nikic commented on May 3, 2024

Closing this in favor of #170 which links to a more up-to-date implementation.

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.