[noop] push by aeagle22206 - Mid-way through a spike with using Protocol Buffers as the serialized ... on 2009-12-11 06:05 GMT

1 view
Skip to first unread message

no...@googlecode.com

unread,
Dec 11, 2009, 1:06:32 AM12/11/09
to noop-c...@googlegroups.com
Revision: fb8d40a54c
Author: Alex Eagle <alex...@google.com>
Date: Thu Dec 10 22:05:16 2009
Log: Mid-way through a spike with using Protocol Buffers as the serialized
form of the AST. It's actually quite a nice match, here's an example of
what the text serialization looks like:

name: "CmdLineExample"
documentation: "used to read cmd line args"
binding {
name: "example.AppBinding"
documentation: "Contains bindings"
block {
bind {
type: "noop.Application"
to: "example.CmdLineArgs"
}
}
}
concrete_class {
name: "example.CmdLineArgs"
documentation: "Demonstrates reading command line arguments in Noop."
super_type: "noop.Application"
property {
name: "console"
type: "noop.system.Console"
}
property {
name: "args"
type: "noop.system.RawCommandLineArgs"
}
method {
signature {
name: "main"
documentation: "the entry point of the application"
return_type: "noop.Int"
}
block {
statement {
type: METHOD_INVOCATION
methodInvocation {
lhs: "console"
method: "println"
argument: "hello"
}
}
statement {
type: RETURN_STATEMENT
returned: "0"
}
}
}
unittest {
description: "should print the first argument"
}
unittest {
description: "should return zero"
}
}
http://code.google.com/p/noop/source/detail?r=fb8d40a54c

Added:
/core/src/main/proto/noop.proto
/core/src/test/java/noop/model/proto/ProtoBufferSpike.java
Modified:
/buildfile

=======================================
--- /dev/null
+++ /core/src/main/proto/noop.proto Thu Dec 10 22:05:16 2009
@@ -0,0 +1,124 @@
+// Generate the java file with:
+// protoc core/src/main/proto/noop.proto --java_out=core/src/main/java
+// TODO(alexeagle): have buildr run the proto compiler
+
+option java_package = "noop.model.proto";
+
+enum Modifier {
+ MUTABLE = 1;
+}
+
+message Module {
+ required string name = 1;
+ optional string documentation = 2;
+ repeated Binding binding = 3;
+ repeated Alias alias = 4;
+ repeated Interface interface = 5;
+ repeated ConcreteClass concrete_class = 6;
+}
+
+message ConcreteClass {
+ required string name = 1;
+ optional string documentation = 2;
+ repeated string super_type = 3;
+ repeated Property property = 4;
+ repeated Method method = 5;
+ repeated Unittest unittest = 6;
+}
+
+message Property {
+ required string name = 1;
+ required string type = 2;
+ repeated Modifier modifier = 3;
+ optional string documentation = 4;
+}
+
+message Binding {
+ required string name = 1;
+ optional string documentation = 2;
+ repeated Modifier modifier = 3;
+ required BindingBlock block = 4;
+}
+
+message Interface {
+ required string name = 1;
+ optional string documentation = 2;
+ repeated MethodSignature method = 3;
+}
+
+message Alias {
+ required string name = 1;
+ optional string documentation = 2;
+ required string of = 3;
+}
+
+message MethodSignature {
+ required string name = 1;
+ optional string documentation = 2;
+ repeated string return_type = 3;
+ repeated Property argument = 4;
+}
+
+message Method {
+ required MethodSignature signature = 1;
+ required Block block = 2;
+}
+
+message BindingBlock {
+ repeated BindOperator bind = 1;
+}
+
+message BindOperator {
+ required string type = 1;
+ required Expression boundTo = 2;
+}
+
+message Block {
+ repeated Statement statement = 1;
+}
+
+message Statement {
+ enum Type {
+ METHOD_INVOCATION = 1;
+ RETURN_STATEMENT = 2;
+ }
+ // identifies which sort of statement this is
+ required Type type = 1;
+
+ // only one of the following should be set
+ optional MethodInvocation methodInvocation = 2;
+ optional Expression returned = 3;
+}
+
+message MethodInvocation {
+ required Expression lhs = 1;
+ required string method = 2;
+ repeated Expression argument = 3;
+}
+
+message Expression {
+ enum Type {
+ EVALUATED = 1;
+ BOOLEAN_LITERAL = 2;
+ STRING_LITERAL = 3;
+ NUMBER_LITERAL = 4;
+ IDENTIFIER = 5;
+ CONDITIONAL = 6;
+ DEREFERENCE = 7;
+ INDEX = 8;
+ }
+ // identifies what sort of expression this is
+ required Type type = 1;
+
+ // only some of the following should be set, depending on type
+ optional bool booleanVal = 2;
+ optional string stringVal = 3;
+ optional uint32 numberVal = 4; // TODO(alexeagle): need to store floats
too
+ optional Expression lhs = 5;
+ optional Expression rhs = 6;
+}
+
+message Unittest {
+ required string description = 1;
+ optional string documentation = 2;
+}
=======================================
--- /dev/null
+++ /core/src/test/java/noop/model/proto/ProtoBufferSpike.java Thu Dec 10
22:05:16 2009
@@ -0,0 +1,60 @@
+package noop.model.proto;
+
+import com.google.protobuf.InvalidProtocolBufferException;
+import com.google.protobuf.TextFormat;
+import com.google.protobuf.TextFormat.ParseException;
+import noop.model.proto.Noop.*;
+
+import static noop.model.proto.Noop.Expression.Type.IDENTIFIER;
+import static noop.model.proto.Noop.Expression.Type.STRING_LITERAL;
+import static noop.model.proto.Noop.Statement.Type.METHOD_INVOCATION;
+import static noop.model.proto.Noop.Statement.Type.RETURN_STATEMENT;
+
+/**
+ * Utility class used for experimenting with serializing the Noop AST to
protocol buffer.
+ * TODO(alexeagle): remove when/if it's no longer useful
+ * @author alex...@google.com (Alex Eagle)
+ */
+public class ProtoBufferSpike {
+ public static void main(String[] args) throws
InvalidProtocolBufferException, ParseException {
+ Module myModule = Module.newBuilder()
+ .setName("CmdLineExample")
+ .setDocumentation("used to read cmd line args")
+ .addBinding(Binding.newBuilder()
+ .setName("example.AppBinding")
+ .setDocumentation("Contains bindings")
+ .setBlock(BindingBlock.newBuilder()
+ .addBind(BindOperator.newBuilder()
+ .setType("noop.Application")
+ .setTo(Expression.newBuilder().setType(IDENTIFIER).setStringVal("example.CmdLineArgs")))))
+ .addConcreteClass(ConcreteClass.newBuilder()
+ .setName("example.CmdLineArgs")
+ .setDocumentation("Demonstrates reading command line arguments
in Noop.")
+ .addSuperType("noop.Application")
+ .addProperty(Property.newBuilder().setName("console").setType("noop.system.Console"))
+ .addProperty(Property.newBuilder().setName("args").setType("noop.system.RawCommandLineArgs"))
+ .addMethod(Method.newBuilder()
+ .setSignature(MethodSignature.newBuilder()
+ .setName("main")
+ .setDocumentation("the entry point of the application")
+ .addReturnType("noop.Int"))
+ .setBlock(Block.newBuilder()
+ .addStatement(Statement.newBuilder()
+ .setType(METHOD_INVOCATION)
+ .setMethodInvocation(MethodInvocation.newBuilder()
+ .setLhs(Expression.newBuilder().setType(IDENTIFIER).setStringVal("console"))
+ .setMethod("println")
+ .addArgument(Expression.newBuilder().setType(STRING_LITERAL).setStringVal("hello"))))
+ .addStatement(Statement.newBuilder()
+ .setType(RETURN_STATEMENT)
+ .setReturned(Expression.newBuilder().setNumberVal(0)))))
+ .addUnittest(Unittest.newBuilder().setDescription("should
print the first argument"))
+ .addUnittest(Unittest.newBuilder().setDescription("should
return zero"))
+ ).build();
+ String serialized = myModule.toString();
+ System.out.println("myModule = " + serialized);
+ Noop.Module.Builder builder = Module.newBuilder();
+ TextFormat.merge(serialized, builder);
+ System.out.println("builder.build().getName() = " +
builder.build().getName());
+ }
+}
=======================================
--- /buildfile Wed Dec 9 00:03:18 2009
+++ /buildfile Thu Dec 10 22:05:16 2009
@@ -29,7 +29,8 @@
SLF4J =
["org.slf4j:slf4j-api:jar:1.5.6", "org.slf4j:slf4j-simple:jar:1.5.6"]
GUICE = ["aopalliance:aopalliance:jar:1.0",
"com.google.inject:guice:jar:2.0", "com.google.inject.extensions:guice-assisted-inject:jar:2.0"
]
-JVYAML = ["net.java.dev:jvyaml:jar:0.2.1"]
+PROTO = ["com.google.protobuf:protobuf-java:jar:2.2.0"]
+
# Force Buildr Antlr integration to use the version we specify
Buildr::ANTLR::REQUIRES.clear
Buildr::ANTLR::REQUIRES.concat(ANTLR)
@@ -39,18 +40,19 @@

manifest["Implementation-Vendor"] = COPYRIGHT

- define "grammar" do
- antlr = antlr([_('src/main/antlr3/noop/grammar/antlr/Doc.g'), \
- _('src/main/antlr3/noop/grammar/antlr/Noop.g'), \
- _('src/main/antlr3/noop/grammar/antlr/NoopAST.g')],
- :in_package=>'noop.grammar.antlr')
- compile.from(antlr).
- with [project("core"), ANTLR, SLF4J]
- package :jar
- end
+# The grammar will need some work to produce the new AST
+# define "grammar" do
+# antlr = antlr([_('src/main/antlr3/noop/grammar/antlr/Doc.g'), \
+# _('src/main/antlr3/noop/grammar/antlr/Noop.g'), \
+# _('src/main/antlr3/noop/grammar/antlr/NoopAST.g')],
+# :in_package=>'noop.grammar.antlr')
+# compile.from(antlr).
+# with [project("core"), ANTLR, SLF4J]
+# package :jar
+# end

define "core" do
- compile.with [SLF4J, JVYAML]
+ compile.with [SLF4J, PROTO]
package :jar
end

@@ -68,7 +70,7 @@
# TODO - only want examples as a test resource
resources.from [_('src/main/noop'), project("examples")._('noop')]
package(:jar).with(:manifest=>{'Main-Class'
=> 'noop.interpreter.InterpreterMain'})
- compile.with [project("core"), ANTLR_RUNTIME, SLF4J, GUICE]
+ compile.with [project("core"), ANTLR_RUNTIME, SLF4J, GUICE, PROTO]
package(:zip).
include(compile.dependencies, :path=>'lib').
include(_("target/#{id}-#{version}.jar"), :path=>'lib').
Reply all
Reply to author
Forward
0 new messages