Skip to content

Commit

Permalink
Support namespacing with packages and inner classes
Browse files Browse the repository at this point in the history
  • Loading branch information
chrisdolan committed Feb 20, 2009
1 parent 4c4df21 commit dc4fd26
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 18 deletions.
13 changes: 7 additions & 6 deletions lib/Perk/Grammar.pm
Expand Up @@ -65,7 +65,7 @@ rule compilationUnit {
}

rule packageDeclaration {
'package' <qualifiedName> ';'
'package' $<package>=[<qualifiedName>] ';' {*}
}

rule importDeclaration {
Expand Down Expand Up @@ -109,8 +109,9 @@ rule normalClassDeclaration {
'class' <Identifier> <typeParameters>?
['extends' <type>]?
['implements' <typeList>]?
<classBody>
{*}
{*} #= start
<classBody>
{*} #= end
}

rule typeParameters {
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -233,7 +234,7 @@ rule methodDeclaratorRest {

rule voidMethodDeclaratorRest {
<formalParameters> ['throws' <qualifiedNameList>]?
[ <methodBody> | ';' ]
[ <methodBody> | ';' ]
{*}
}

Expand Down Expand Up @@ -475,7 +476,7 @@ rule block {
'{' <blockStatement>* '}' {*}
}

rule blockStatement {
rule blockStatement {
| <localVariableDeclarationStatement> {*} #= localVariableDeclarationStatement
| <classOrInterfaceDeclaration> {*} #= classOrInterfaceDeclaration
| <statement> {*} #= statement
Expand Down
52 changes: 40 additions & 12 deletions lib/Perk/Grammar/Actions.pm
Expand Up @@ -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> );
}
Expand All @@ -36,7 +39,8 @@ method compilationUnit($/) {
}

method packageDeclaration($/) {
say('package');
my $package = ~$<package>;
@package = $package.split('.');
}

method annotation($/) {
Expand Down Expand Up @@ -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($/) {
Expand Down
10 changes: 10 additions & 0 deletions t/com/example/Namespace.java
@@ -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.