[Parseperl-discuss] What should 5.10's switch statement parse like?
Brought to you by:
adamkennedy
From: Elliot S. <pe...@ga...> - 2008-09-21 06:55:11
|
Let's discuss the following code: given ($foo) { when (@blah) { } default { } } I would have thought that given would be a compound statement (especially in the face of the ability to use when/default in foreach loops); however, perlsyn does not include it in its list of compound statements, so it sounds like we'd need a new subclass of Statement for it. (Also, this would help differentiation in Perl::Critic, especially in the determination of whether a specific Policy applies to an Element.) Or we could ignore perlsyn's omission and treat it as Compound. Should the parentheses be a Structure::Condition? Or something analogous to Structure::ForLoop? (Again, from the P::C perspective, having a new class would be better.) Then, the when/default. The actual statements, like the given, could be Statement::Compound instances, and the parentheses after when could just be Conditions again. Even if there's a new class, the when and default could be instances of the same one, since perlsyn says that default is precisely equivalent to "when (1 == 1) { ... }". So, ignoring whitespace tokens, as I see it, here's the most conservative way of this parsing: PPI::Document PPI::Statement::Compound PPI::Token::Word 'given' PPI::Structure::Condition ( ... ) PPI::Statement::Expression PPI::Token::Symbol '$foo' PPI::Structure::Block { ... } PPI::Statement::Compound PPI::Token::Word 'when' PPI::Structure::Condition ( ... ) PPI::Statement::Expression PPI::Token::Symbol '@blah' PPI::Structure::Block { ... } PPI::Statement::Compound PPI::Token::Word 'default' PPI::Structure::Block { ... } OTOH, here's the most favorable structure from P::C's perspective (modulo the class names changing): PPI::Document PPI::Statement::Switch PPI::Token::Keyword 'given' PPI::Structure::Given ( ... ) PPI::Statement::Expression PPI::Token::Symbol '$foo' PPI::Structure::Block { ... } PPI::Statement::When PPI::Token::Keyword 'when' PPI::Structure::Match ( ... ) PPI::Statement::Expression PPI::Token::Symbol '@blah' PPI::Structure::Block { ... } PPI::Statement::When PPI::Token::Keyword 'default' PPI::Structure::Block { ... } Yes, I'm thinking of trying to implement this. First, I need to figure out the parser and see whether I can fix some existing parsing problems. |