Page MenuHomePhabricator

Improve parsing of namespaces
ClosedPublic

Authored by joshuaspence on Nov 17 2015, 7:29 AM.
Tags
None
Referenced Files
F14017005: D14498.diff
Mon, Nov 4, 1:09 PM
F14005538: D14498.id35071.diff
Sun, Oct 27, 3:09 PM
F13995228: D14498.diff
Wed, Oct 23, 11:30 AM
F13994063: D14498.id35083.diff
Wed, Oct 23, 3:29 AM
F13984991: D14498.id.diff
Sun, Oct 20, 5:23 PM
F13979262: D14498.id.diff
Sat, Oct 19, 3:36 AM
F13978611: D14498.diff
Sat, Oct 19, 12:09 AM
F13977852: D14498.id.diff
Fri, Oct 18, 8:21 PM

Details

Summary

Currently PHP namespaces are parsed in two different ways, depending on the syntax used. For example, consider the following example:

<?php

namespace A;

class B {}

namespace A\B\C {
 class D {}
}

namespace {
  class A {}
}

This is currently parsed by XHPAST as follows (see https://secure.phabricator.com/xhpast/view/915/):

xhpast-before.png (799×297 px, 28 KB)

This generally makes it difficult to determine the namespace in which a class is declared (unless the namespace is defined using the namespace { ... } syntax, which is somewhat rare). Instead, always parse namespaces as if they were of the namespace { ... } form.

Test Plan

Added unit tests. Also inspected the AST after this change:

xhpast-after.png (708×243 px, 25 KB)

Diff Detail

Repository
rPHU libphutil
Lint
Lint Not Applicable
Unit
Tests Not Applicable

Event Timeline

joshuaspence retitled this revision from to Improve parsing of namespaces.
joshuaspence updated this object.
joshuaspence edited the test plan for this revision. (Show Details)
joshuaspence added a reviewer: epriestley.
JolanYacht added a subscriber: JolanYacht.
  • 邀请您做代码查看**谢谢 thx
epriestley edited edge metadata.

Unrelated, but I wonder if we can find a better way to represent the test cases -- dumping the JSON turns out to be totally unreadable.

Maybe we could just sort of mark up the file in some way so that it was approximately human-readable.

This revision is now accepted and ready to land.Nov 17 2015, 3:18 PM

One thing that would slightly help readability would be to use token names rather than token IDs.

This revision was automatically updated to reflect the committed changes.

The mixed syntax is actually invalid. (Maybe known, just saw this one fly by)

That should be fine, we're just parsing the two different forms in a similar way, not requiring then to appear in the same file.

That said, maybe top_statement should be top_statement_list? Do we parse this file correctly (with no statements)?

<?php namespace x;

That should be fine, we're just parsing the two different forms in a similar way, not requiring then to appear in the same file.

That said, maybe top_statement should be top_statement_list? Do we parse this file correctly (with no statements)?

<?php namespace x;

No we don't handle that correctly. Let me see if I can fix that...

That file is pretty silly, of course, but it'd be nice to handle it.

That said, maybe top_statement should be top_statement_list? Do we parse this file correctly (with no statements)?

It's not quite that simple...

bison --verbose -Wall --defines=parser.yacc.hpp --output=parser.yacc.cpp parser.y
parser.y: error: shift/reduce conflicts: 86 found, 5 expected
Makefile:57: recipe for target 'parser.yacc.hpp' failed
make: *** [parser.yacc.hpp] Error 1