Permalink
Browse files

Support namespacing with packages and inner classes

  • Loading branch information...
1 parent 4c4df21 commit dc4fd265395e6f24cdd2afee750fc2306c7a51c0 @chrisdolan committed Feb 20, 2009
Showing with 57 additions and 18 deletions.
  1. +7 −6 lib/Perk/Grammar.pm
  2. +40 −12 lib/Perk/Grammar/Actions.pm
  3. +10 −0 t/com/example/Namespace.java
View
@@ -65,7 +65,7 @@ rule compilationUnit {
}
rule packageDeclaration {
- 'package' <qualifiedName> ';'
+ 'package' $<package>=[<qualifiedName>] ';' {*}
}
rule importDeclaration {
@@ -109,8 +109,9 @@ rule normalClassDeclaration {
'class' <Identifier> <typeParameters>?
['extends' <type>]?
['implements' <typeList>]?
- <classBody>
- {*}
+ {*} #= start
+ <classBody>
+ {*} #= end
}
rule typeParameters {
@@ -175,7 +176,7 @@ rule classBodyDeclaration {
rule memberDecl {
| <genericMethodOrConstructorDecl> {*} #= genericMethodOrConstructorDecl
- | 'void' <Identifier> <voidMethodDeclaratorRest> {*} #= voidMethodDeclaratorRest
+ | 'void' <Identifier> <voidMethodDeclaratorRest> {*} #= voidMethodDeclaratorRest
| <memberDeclaration> {*} #= memberDeclaration
| <Identifier> <constructorDeclaratorRest> {*} #= constructorDeclaratorRest
| <interfaceDeclaration> {*} #= interfaceDeclaration
@@ -233,7 +234,7 @@ rule methodDeclaratorRest {
rule voidMethodDeclaratorRest {
<formalParameters> ['throws' <qualifiedNameList>]?
- [ <methodBody> | ';' ]
+ [ <methodBody> | ';' ]
{*}
}
@@ -475,7 +476,7 @@ rule block {
'{' <blockStatement>* '}' {*}
}
-rule blockStatement {
+rule blockStatement {
| <localVariableDeclarationStatement> {*} #= localVariableDeclarationStatement
| <classOrInterfaceDeclaration> {*} #= classOrInterfaceDeclaration
| <statement> {*} #= statement
@@ -16,6 +16,9 @@ value of the comment is passed as the second argument to the method.
class Perk::Grammar::Actions;
+our @package;
+our @classes;
+
method TOP($/) {
make $( $<compilationUnit> );
}
@@ -36,7 +39,8 @@ method compilationUnit($/) {
}
method packageDeclaration($/) {
- say('package');
+ my $package = ~$<package>;
+ @package = $package.split('.');
}
method annotation($/) {
@@ -99,17 +103,41 @@ method classDeclaration($/, $key) {
make $( $/{$key} );
}
-method normalClassDeclaration($/) {
- my $class := $( $<classBody> );
- $class.loadinit().push(
- PAST::Op.new(
- :pasttype('call'),
- :name('!create_class'),
- ~$<Identifier>
- )
- );
- $class.namespace(~$<Identifier>);
- make $class;
+method normalClassDeclaration($/, $key) {
+ if $key eq 'start' {
+ my @ns = self._compute_local_ns(~$<Identifier>);
+ @classes.push(@ns.join('.'));
+ } else {
+ @classes.pop;
+ my @ns = self._compute_local_ns(~$<Identifier>);
+ my $classname = @ns.join('.');
+ my $class := $( $<classBody> );
+ $class.loadinit().push(
+ PAST::Op.new(
+ :pasttype('call'),
+ :name('!create_class'),
+ $classname
+ )
+ );
+ $class.namespace(@ns);
+ make $class;
+ }
+}
+method _compute_local_ns(Str $name) {
+ my @ns;
+ if $name ~~ m/\./ {
+ # Fully qualified class
+ @ns = $name.split('.');
+ } elsif @classes.elems > 0 {
+ # Inner class
+ @ns = @classes[*-1].split('.');
+ @ns.push($name);
+ } else {
+ # Prepend package
+ @ns = @package;
+ @ns.push($name);
+ }
+ return @ns;
}
method classBody($/) {
@@ -0,0 +1,10 @@
+package com.example;
+
+public class Namespace {
+
+ class Inner {
+ }
+
+ public static void main(String[] args) {
+ }
+}

0 comments on commit dc4fd26

Please sign in to comment.