Skip to content

Instantly share code, notes, and snippets.

/.p6

Created July 29, 2016 14:25
Show Gist options
  • Save anonymous/132e16cdfca581533647990438495906 to your computer and use it in GitHub Desktop.
Save anonymous/132e16cdfca581533647990438495906 to your computer and use it in GitHub Desktop.
grammar that hangs, but works when tracer is used
#!/usr/bin/env perl6
use v6;
# XXX this makes it work
#use Grammar::Tracer;
grammar JavaPackagesAndImports {
rule TOP { <compilationUnit> }
# switching this for the next rule also makes it work
# rule compilationUnit { <packageDeclaration>? <STUFF>? }
rule compilationUnit { <packageDeclaration>? <importDeclaration>* <STUFF>? }
rule packageDeclaration { 'package' <package> ';' }
rule package { <Identifier> [ '.' <Identifier> ]* }
rule importDeclaration { <singleTypeImportDeclaration> | <typeImportOnDemandDeclaration> | <singleStaticImportDeclaration> | <staticImportOnDemandDeclaration> }
rule singleTypeImportDeclaration { 'import' <typeName> ';' }
rule typeImportOnDemandDeclaration { 'import' <packageOrTypeName> '.' '*' ';' }
rule singleStaticImportDeclaration { 'import' 'static' <typeName> '.' <Identifier> ';' }
rule staticImportOnDemandDeclaration { 'import' 'static' <typeName> '.' '*' ';' }
rule Identifier { <[a..zA..Z\$_]><[a..zA..Z0..9\$_]>* }
rule typeName { <Identifier> | <packageOrTypeName> '.' <Identifier> }
rule packageOrTypeName { <Identifier> | <packageOrTypeName> '.' <Identifier> }
token STUFF { .* }
}
my $code = q:heredoc/EOL/;
package com.cisco.spvss.p2o.datamodel.serialization;
import com.cisco.spvss.p2o.datamodel.common.BaseDataModel;
/** common interface for per-datamodel serializers */
public interface BaseModelSerializer {
/** take a data model item and return a string representation. returns null if the item type
* is not supported by this serializer, throws in case of technical problems */
String serialize(BaseDataModel item) throws Exception;
/** take a string representation and return a data model item. returns null if the item type
* is not supported by this serializer, throws in case of technical problems */
BaseDataModel deserialize(String input) throws Exception;
}
EOL
JavaPackagesAndImports.parse($code);
say "done";
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment