Charj: fix namespace handling to use symbol table correctly
authorAaron Becker <akbecker@gmail.com>
Fri, 21 May 2010 21:40:38 +0000 (16:40 -0500)
committerAaron Becker <akbecker@gmail.com>
Fri, 21 May 2010 21:40:38 +0000 (16:40 -0500)
src/langs/charj/src/charj/translator/Charj.stg
src/langs/charj/src/charj/translator/CharjEmitter.g
src/langs/charj/src/charj/translator/CharjSemantics.g
src/langs/charj/src/charj/translator/ClassSymbol.java
src/langs/charj/src/charj/translator/PackageScope.java
src/langs/charj/src/charj/translator/SymbolTable.java

index 4eb16f65f89175d47c58434795e687dbfd427f8a..d0c6b24e7300c8624fb7f366e77cabdf7f6234ea 100644 (file)
@@ -23,7 +23,7 @@ charjSource_ci(pd, ids, tds, debug) ::=
 >>
 
 
-charjSource_h(pd, ids, tds, cb, debug) ::= 
+charjSource_h(pd, ids, tds, debug) ::=
 <<
 <if(debug)>/* \<CHARJ_SOURCE> */<endif>
 /**************************************************************************
@@ -36,31 +36,22 @@ charjSource_h(pd, ids, tds, cb, debug) ::=
 <pd>
 <ids>
 <tds>
-<cb>
 
 <if(debug)>/* \</CHARJ_SOURCE> */<endif>
 
 >>
 
 
-charjSource_cc(pd, ids, tds, cb, debug) ::= 
+charjSource_cc(pd, ids, tds, debug) ::=
 <<
 <if(debug)>/* \<CHARJ_SOURCE> */<endif>
 <ids>
 <pd>
 <tds>
-<cb>
 <if(debug)>/* \</CHARJ_SOURCE> */<endif>
 
 >>
 
-packageDeclaration_cc_h(ids) ::= 
-<<
-<if(debug)>/* \<packageDeclaration> */<endif>
-namespace <ids; separator=" { namespace "> {
-<if(debug)>/* \</packageDeclaration> */<endif>
->>
-
 importDeclaration_cc_h(inc_id, use_id) ::= 
 <<
 <if(debug)>/* \<importDeclaration> */<endif>
@@ -80,26 +71,24 @@ interfaceExtends(ts) ::=
 : public <ts; separator=", public ">
 >>
 
-
-classDeclaration_h(ident, ext, csds) ::=
+classDeclaration_h(sym, ident, ext, csds) ::=
 <<
-<if(debug)>/* \<typeDeclaration> */<endif>
+<sym.NamespaceOpeningString>
 <if(ext)>
-class <ident>: public <ext> {
+class <ident> : public <ext> {
 <else>
 class <ident> {
 <endif>
 
     <csds; separator="\n">
 };
-
-<if(debug)>/* \</typeDeclaration> */<endif>
+<sym.NamespaceClosingString>
 
 >>
 
-classDeclaration_cc(ident, ext, csds) ::=
+classDeclaration_cc(sym, ident, ext, csds) ::=
 <<
-<if(debug)>/* \<typeDeclaration> */<endif>
+<sym.NamespaceOpeningString>
 <if(ext)>
 class <ident>: public <ext> {
 <else>
@@ -108,42 +97,46 @@ class <ident> {
 
     <csds; separator="\n">
 };
-<if(debug)>/* \</typeDeclaration> */<endif>
+<sym.NamespaceClosingString>
 
 >>
 
-chareDeclaration_cc(ident, ext, csds) ::=
+chareDeclaration_cc(sym, ident, ext, csds) ::=
 <<
 
 #include "<ident>.decl.h"
+<sym.NamespaceOpeningString>
 /* superclass: <ext> */
 class <ident>: public CBase_<ident> {
     <csds; separator="\n">
 };
+<sym.NamespaceClosingString>
 #include "<ident>.def.h"
 >>
 
-chareDeclaration_h(ident, ext, csds) ::=
+chareDeclaration_h(sym, ident, ext, csds) ::=
 <<
 #include "<ident>.decl.h"
+<sym.NamespaceOpeningString>
 /* superclass: <ext> */
 class <ident>: public CBase_<ident> {
     <csds; separator="\n">
 };
+<sym.NamespaceClosingString>
 >>
 
-chareDeclaration_ci(chareType, arrayDim, ident, ext, csds) ::=
+chareDeclaration_ci(sym, chareType, arrayDim, ident, ext, csds) ::=
 <<
 <if(debug)>/* \<typeDeclaration> */<endif>
 module <ident> {
 <if(ext)>
     extern module <ext>;
-    <chareType><if(arrayDim)> [<arrayDim>]<endif> <ident>: <ext> {
-<else>
-    <chareType><if(arrayDim)> [<arrayDim>]<endif> <ident> {
 <endif>
+    <sym.NamespaceOpeningString>
+    <chareType><if(arrayDim)> [<arrayDim>]<endif> <ident><if(ext)> : <ext><endif> {
         <csds; separator="\n">
     }
+    <sym.NamespaceClosingString>
 }
 
 <if(debug)>/* \</typeDeclaration> */<endif>
index ae115765020196eb4f885101f1e61b305a33733f..1d60e9a1214eb880ee5aab45ded1487d92590f34 100644 (file)
@@ -19,12 +19,12 @@ package charj.translator;
 
 
 @members {
-    SymbolTable symtab_ = null;
+    SymbolTable symtab = null;
 
-    PackageScope currentPackage_ = null;
-    ClassSymbol currentClass_ = null;
-    MethodSymbol currentMethod_ = null;
-    LocalScope currentLocalScope_ = null;
+    PackageScope currentPackage = null;
+    ClassSymbol currentClass = null;
+    MethodSymbol currentMethod = null;
+    LocalScope currentLocalScope = null;
 
     Translator translator_;
     OutputMode mode_;
@@ -78,30 +78,18 @@ package charj.translator;
 // Starting point for parsing a Charj file.
 charjSource[SymbolTable symtab, OutputMode m]
 @init {
-    this.symtab_ = symtab;
+    this.symtab = symtab;
     this.translator_ = symtab.translator;
     this.mode_ = m;
-    String closingBraces = "";
 }
     :   ^(CHARJ_SOURCE (p=packageDeclaration)? 
         (i+=importDeclaration)* 
         (t=typeDeclaration))
-        {
-            // construct string of }'s to close namespace 
-            if ($p.st != null) {
-                String temp_p = $p.st.toString();
-                for (int idx=0; idx<temp_p.length(); ++idx) {
-                    if (temp_p.charAt(idx) == '{') {
-                        closingBraces += "} ";
-                    }
-                }
-            }
-        }
         -> {emitCC()}? charjSource_cc(
-            pd={$p.st}, ids={$i}, tds={$t.st}, cb={closingBraces}, debug={debug()})
+            pd={$p.st}, ids={$i}, tds={$t.st}, debug={debug()})
         -> {emitCI()}? charjSource_ci(pd={$p.st}, ids={$i}, tds={$t.st}, debug={debug()})
         -> {emitH()}? charjSource_h(
-            pd={$p.st}, ids={$i}, tds={$t.st}, cb={closingBraces}, debug={debug()})
+            pd={$p.st}, ids={$i}, tds={$t.st}, debug={debug()})
         ->
     ;
 
@@ -109,12 +97,7 @@ packageDeclaration
 @init { 
     List<String> names = null; 
 }
-    :   ^('package' qualifiedIdentifier)  {
-            names =  java.util.Arrays.asList(
-                    $qualifiedIdentifier.text.split("[.]"));
-        }
-        -> {(emitCC() || emitH())}? packageDeclaration_cc_h(
-            ids={names})
+    :   ^('package' qualifiedIdentifier)
         ->
     ;
     
@@ -137,11 +120,16 @@ importDeclaration
     
 typeDeclaration
     :   ^('class' IDENT (^('extends' su=type))? (^('implements' type+))? (csds+=classScopeDeclaration)*)
+        {
+            currentClass = (ClassSymbol)$IDENT.symbol;
+        }
         -> {emitCC()}? classDeclaration_cc(
+                sym={currentClass},
                 ident={$IDENT.text}, 
                 ext={$su.st}, 
                 csds={$csds})
-        -> {emitH()}? classDeclaration_h(
+        -> {emitH()}?  classDeclaration_h(
+                sym={currentClass},
                 ident={$IDENT.text}, 
                 ext={$su.st}, 
                 csds={$csds})
@@ -151,28 +139,45 @@ typeDeclaration
     |   ^('enum' IDENT (^('implements' type+))? classScopeDeclaration*)
         -> template(t={$text}) "/*ENUM-not implemented*/ <t>"
     |   ^(chareType IDENT (^('extends' type))? (^('implements' type+))? classScopeDeclaration*)
+        {
+            currentClass = (ClassSymbol)$IDENT.symbol;
+        }
         -> {emitCC()}? chareDeclaration_cc(
+                sym={currentClass},
                 ident={$IDENT.text}, 
                 ext={$su.st}, 
                 csds={$csds})
         -> {emitCI()}? chareDeclaration_ci(
+                sym={currentClass},
                 chareType={$chareType.st},
                 arrayDim={null},
                 ident={$IDENT.text}, 
                 ext={$su.st}, 
                 csds={$csds})
         -> {emitH()}? chareDeclaration_h(
+                sym={currentClass},
                 ident={$IDENT.text}, 
                 ext={$su.st}, 
                 csds={$csds})
         ->
     |   ^('chare_array' ARRAY_DIMENSION IDENT (^('extends' type))? (^('implements' type+))? classScopeDeclaration*)
+        -> {emitCC()}? chareDeclaration_cc(
+                sym={currentClass},
+                ident={$IDENT.text}, 
+                ext={$su.st}, 
+                csds={$csds})
         -> {emitCI()}? chareDeclaration_ci(
+                sym={currentClass},
                 chareType={"array"},
                 arrayDim={$ARRAY_DIMENSION.text.toUpperCase()},
                 ident={$IDENT.text}, 
                 ext={$su.st}, 
                 csds={$csds})
+        -> {emitH()}? chareDeclaration_h(
+                sym={currentClass},
+                ident={$IDENT.text}, 
+                ext={$su.st}, 
+                csds={$csds})
         ->
     ;
 
index 8b25e131aaad7ced46c62e9127ed75e85f0956cc..39fe290f562fc99c4cd3dac6ffe498db7e551b29 100644 (file)
@@ -119,10 +119,11 @@ importDeclarations returns [List<CharjAST> packageNames]
 
 typeDeclaration[List<CharjAST> imports] returns [ClassSymbol sym]
 scope ScopeStack; // top-level type scope
-    :   ^('class' IDENT (^('extends' type))? (^('implements' type+))? classScopeDeclaration*)
+    :   ^((('class')|(chareType)|('chare_array' ARRAY_DIMENSION)) IDENT
+            (^('extends' parent=type))? (^('implements' type+))? classScopeDeclaration*)
         {
             Scope outerScope = $ScopeStack[-1]::current;
-            $sym = new ClassSymbol(symtab, $IDENT.text, null, outerScope);
+            $sym = new ClassSymbol(symtab, $IDENT.text, outerScope.resolveType($parent.text), outerScope);
             outerScope.define($sym.name, $sym);
             currentClass = $sym;
             $sym.definition = $typeDeclaration.start;
@@ -133,8 +134,6 @@ scope ScopeStack; // top-level type scope
         }
     |   ^('interface' IDENT (^('extends' type+))?  interfaceScopeDeclaration*)
     |   ^('enum' IDENT (^('implements' type+))? enumConstant+ classScopeDeclaration*)
-    |   ^(chareType IDENT (^('extends' type))? (^('implements' type+))? classScopeDeclaration*)
-    |   ^('chare_array' ARRAY_DIMENSION IDENT (^('extends' type))? (^('implements' type+))? classScopeDeclaration*)
     ;
 
 chareType
index 270c1901798997e088858bcaca4b80a896c50340..c5e5835fd9cc1f24f922f6d39630ddb6082db4e1 100644 (file)
@@ -243,4 +243,23 @@ public class ClassSymbol extends SymbolWithScope implements Scope {
         return name;
     }
 
+    public String getNamespaceOpeningString() {
+        Scope currentScope = scope;
+        String namespace = "";
+        while (currentScope.getEnclosingScope() != null) {
+            namespace = "namespace " + currentScope.getScopeName() + " { " + namespace;
+            currentScope = currentScope.getEnclosingScope();
+        }
+        return namespace;
+    }
+
+    public String getNamespaceClosingString() {
+        Scope currentScope = scope;
+        String namespace = "";
+        while (currentScope.getEnclosingScope() != null) {
+            namespace += "} ";
+            currentScope = currentScope.getEnclosingScope();
+        }
+        return namespace;
+    }
 }
index 27dc6ddbc963b185dbf59c137df60fafd6bf6b70..1bb5706fe0abffa35909737d53fa993c6f6ce06e 100644 (file)
@@ -29,6 +29,7 @@ public class PackageScope extends SymbolWithScope {
      *  be found.
      */
     public ClassSymbol resolveType(String type) {
+        if (type == null) return null;
         if (debug()) System.out.println(
                 " PackageScope.resolveType(" + type + 
                 "): examine " + toString());
index 6708d4026cf6d1fac27b559e3ad4f8624cf4794f..eb82c9063639e6b5af7d5fe5ad3c47b2e9f13256 100644 (file)
@@ -114,7 +114,7 @@ public class SymbolTable {
         return defaultPkg;
     }
 
-    /** Find package starting with it's outermost package name.  If
+    /** Find package starting with its outermost package name.  If
      *  not in sym tab, return null.
      */
     public PackageScope resolvePackage(String packageName) {