Permalink
Browse files

[swift-idl-parser] Parse and accept default namespace

  • Loading branch information...
1 parent f7b8381 commit f72656f94487e41b53f8cfbf22d8a0ac8bba04c0 @andrewcox andrewcox committed Mar 19, 2013
@@ -44,22 +44,28 @@ document returns [Document value]
header returns [Header value]
@init {
List<String> includes = new ArrayList<>();
- Map<String, String> namespaces = new HashMap<>();
List<String> cppIncludes = new ArrayList<>();
+ String defaultNamespace = null;
+ Map<String, String> namespaces = new HashMap<>();
}
@after {
- $value = new Header(includes, namespaces, cppIncludes);
+ $value = new Header(includes, cppIncludes, defaultNamespace, namespaces);
}
- : ( include { includes.add($include.value); }
- | namespace { namespaces.put($namespace.language, $namespace.value); }
- | cpp_include { cppIncludes.add($cpp_include.value); }
+ : ( include { includes.add($include.value); }
+ | cpp_include { cppIncludes.add($cpp_include.value); }
+ | default_namespace { defaultNamespace = $default_namespace.value; }
+ | namespace { namespaces.put($namespace.language, $namespace.value); }
)*
;
include returns [String value]
: ^(INCLUDE LITERAL) { $value = $LITERAL.text; }
;
+default_namespace returns [String value]
+ : ^(DEFAULT_NAMESPACE (v=IDENTIFIER | v=LITERAL)) { $value = $v.text; }
+ ;
+
namespace returns [String language, String value]
: ^(NAMESPACE k=IDENTIFIER (v=IDENTIFIER | v=LITERAL)) { $language = $k.text ; $value = $v.text; }
;
@@ -26,6 +26,7 @@ tokens {
INCLUDE;
TYPEDEF;
NAMESPACE;
+ DEFAULT_NAMESPACE;
CPP_INCLUDE;
ENUM;
SENUM;
@@ -76,7 +77,8 @@ include
;
namespace
- : 'namespace' k=IDENTIFIER (v=IDENTIFIER | v=LITERAL) -> ^(NAMESPACE $k $v)
+ : 'namespace' '*' (v=IDENTIFIER | v=LITERAL) -> ^(DEFAULT_NAMESPACE $v)
+ | 'namespace' k=IDENTIFIER (v=IDENTIFIER | v=LITERAL) -> ^(NAMESPACE $k $v)
| 'cpp_namespace' IDENTIFIER -> ^(NAMESPACE IDENTIFIER["cpp"] IDENTIFIER)
| 'php_namespace' IDENTIFIER -> ^(NAMESPACE IDENTIFIER["php"] IDENTIFIER)
;
@@ -73,9 +73,10 @@ public String toString()
public static Document emptyDocument()
{
List<String> includes = emptyList();
- Map<String, String> namespaces = Collections.emptyMap();
List<String> cppIncludes = emptyList();
- Header header = new Header(includes, namespaces, cppIncludes);
+ String defaultNamespace = null;
+ Map<String, String> namespaces = Collections.emptyMap();
+ Header header = new Header(includes, cppIncludes, defaultNamespace, namespaces);
List<Definition> definitions = emptyList();
return new Document(header, definitions);
}
@@ -28,13 +28,15 @@
{
private final List<String> includes;
private final Map<String, String> namespaces;
+ private final String defaultNamespace;
private final List<String> cppIncludes;
- public Header(List<String> includes, Map<String, String> namespaces, List<String> cppIncludes)
+ public Header(List<String> includes, List<String> cppIncludes, String defaultNamespace, Map<String, String> namespaces)
{
this.includes = ImmutableList.copyOf(checkNotNull(includes, "includes"));
- this.namespaces = ImmutableMap.copyOf(checkNotNull(namespaces, "namespaces"));
this.cppIncludes = ImmutableList.copyOf(checkNotNull(cppIncludes, "cppIncludes"));
+ this.defaultNamespace = checkNotNull(defaultNamespace, "defaultNamespace");
+ this.namespaces = ImmutableMap.copyOf(checkNotNull(namespaces, "namespaces"));
}
public List<String> getIncludes()
@@ -44,7 +46,11 @@ public Header(List<String> includes, Map<String, String> namespaces, List<String
public String getNamespace(final String nameSpaceName)
{
- return namespaces.get(nameSpaceName);
+ String namespace = namespaces.get(nameSpaceName);
+ if (namespace == null) {
+ namespace = defaultNamespace;
+ }
+ return namespace;
}
public Map<String, String> getNamespaces()
@@ -57,13 +63,19 @@ public String getNamespace(final String nameSpaceName)
return cppIncludes;
}
+ public String getDefaultNamespace()
+ {
+ return defaultNamespace;
+ }
+
@Override
public String toString()
{
return Objects.toStringHelper(this)
.add("includes", includes)
- .add("namespaces", namespaces)
.add("cppIncludes", cppIncludes)
+ .add("defaultNamespace", defaultNamespace)
+ .add("namespaces", namespaces)
.toString();
}
}

0 comments on commit f72656f

Please sign in to comment.