Permalink
Browse files

Implemented the parsing and use of user-defined types, to some extent.

The biggest thing missing here is error-handling. Once I have a working request argument parser, I need to unit-test the crap out of this Slice parser.
  • Loading branch information...
1 parent 021c6f4 commit 73b93a2c2faeba4de65eb65044d86a1157ca8552 @ajtack ajtack committed Jun 4, 2012
Showing with 234 additions and 133 deletions.
  1. +7 −1 demonstrate_parsing.js
  2. +0 −132 slice.jison
  3. +187 −0 slice/grammar.jison
  4. +8 −0 slice/object.ice
  5. +32 −0 slice/types.js
View
@@ -1,8 +1,14 @@
var fs = require('fs');
var generate_parser = require('jison').Parser
+var ice_types = require('./slice/types');
-fs.readFile('slice.jison', 'utf-8', function(error, raw_grammar) {
+fs.readFile('slice/grammar.jison', 'utf-8', function(error, raw_grammar) {
var parse_slice = generate_parser(raw_grammar);
+ parse_slice.yy = ice_types;
+ fs.readFile('slice/object.ice', 'utf-8', function(error, slice_file) {
+ var parsed_modules = parse_slice.parse(slice_file);
+ console.log(parsed_modules);
+ });
fs.readFile('printer.ice', 'utf-8', function(error, slice_file) {
var parsed_modules = parse_slice.parse(slice_file);
console.log(parsed_modules);
View
@@ -1,132 +0,0 @@
-%lex
-identifier [a-zA-Z]([a-zA-Z0-9]|"_"[a-zA-Z0-9])*
-
-%%
-\s+ /* skip whitespace */
-"{" return 'LEFT_BRACE';
-"}" return 'RIGHT_BRACE';
-"[" return 'LEFT_BRACKET';
-"]" return 'RIGHT_BRACKET';
-"(" return 'LEFT_PARENTHESIS';
-")" return 'RIGHT_PARENTHESIS';
-";" return 'SEMICOLON';
-"\"" return 'QUOTE'
-"," return 'COMMA';
-"out" return 'OUT_PARAMETER_KEYWORD';
-"string" return 'TYPE_STRING';
-"int" return 'TYPE_INT';
-"void" return 'TYPE_VOID';
-interface return 'INTERFACE_KEYWORD';
-module return 'MODULE_KEYWORD';
-{identifier} return 'IDENTIFIER';
-<<EOF>> return 'END_OF_FILE';
-
-/lex
-
-%%
-
-file
- : module END_OF_FILE
- { $$ = {};
- $$[$module.name] = $module.body;
- return $$;
- }
- ;
-
-module
- : MODULE_KEYWORD IDENTIFIER LEFT_BRACE module_children RIGHT_BRACE SEMICOLON
- { $$ = {name: $IDENTIFIER, body: {}};
- for (var child_id in $module_children) {
- var child = $module_children[child_id];
- $$.body[child.name] = child.body;
- };
- }
- ;
-
-interface
- : INTERFACE_KEYWORD IDENTIFIER LEFT_BRACE operations RIGHT_BRACE SEMICOLON
- { $$ = {name: $IDENTIFIER, body: {}};
- for (var operation_index in $operations) {
- var operation = $operations[operation_index];
- $$.body[operation.name] = {arguments: $operation.arguments, returns: $operation.returns};
- }
- }
- ;
-
-interface_with_metadata
- : metadata_block interface
- -> $interface;
- ;
-
-metadata_block
- : LEFT_BRACKET metadata RIGHT_BRACKET
- -> $metadata;
- ;
-
-metadata
- : metadatum COMMA metadata
- -> $metadatum;
- | metadatum
- -> $metadatum;
- ;
-
-metadatum
- : QUOTE IDENTIFIER QUOTE
- -> $IDENTIFIER;
- ;
-
-operations
- : operation operations
- { $operations.unshift($operation);
- $$ = $operations;
- }
- | metadata_block operation operations
- { $operations.unshift($operation);
- $$ = $operations;
- }
- | /* end of sequence */
- -> [];
- ;
-
-operation
- : typename IDENTIFIER LEFT_PARENTHESIS argument_declarations RIGHT_PARENTHESIS SEMICOLON
- -> {name: $IDENTIFIER, arguments: $argument_declarations, returns: {result_type: $typename, output_parameters: []}};
- ;
-
-argument_declarations
- : argument_declaration argument_declarations
- { $argument_declarations.unshift($argument_declaration);
- $$ = $argument_declarations;
- }
- | /* end of sequence */
- -> [];
- ;
-
-argument_declaration
- : typename IDENTIFIER
- -> {type: $typename, name: $IDENTIFIER}
- | metadata_block typename IDENTIFIER
- ;
-
-typename
- : TYPE_STRING
- | TYPE_INT
- | TYPE_VOID
- ;
-
-module_children
- : module module_children
- { $module_children.unshift($1);
- $$ = $module_children;
- }
- | interface module_children
- { $module_children.unshift($1);
- $$ = $module_children;
- }
- | interface_with_metadata module_children
- { $module_children.unshift($1);
- $$ = $module_children;
- }
- | /* end of sequence */
- -> [];
- ;
View
@@ -0,0 +1,187 @@
+%lex
+identifier [a-zA-Z]([a-zA-Z0-9]|"_"[a-zA-Z0-9])*
+
+%%
+\s+ /* skip whitespace */
+"{" return 'LEFT_BRACE';
+"}" return 'RIGHT_BRACE';
+"[" return 'LEFT_BRACKET';
+"]" return 'RIGHT_BRACKET';
+"(" return 'LEFT_PARENTHESIS';
+")" return 'RIGHT_PARENTHESIS';
+"<" return 'LEFT_ALLIGATOR';
+">" return 'RIGHT_ALLIGATOR';
+";" return 'SEMICOLON';
+"\"" return 'QUOTE'
+"," return 'COMMA';
+"out" return 'OUT_PARAMETER_KEYWORD';
+"bool" return 'TYPE_BOOL';
+"string" return 'TYPE_STRING';
+"int" return 'TYPE_INT';
+"void" return 'TYPE_VOID';
+"idempotent" return 'IDEMPOTENT_KEYWORD';
+"interface" return 'INTERFACE_KEYWORD';
+"module" return 'MODULE_KEYWORD';
+"sequence" return 'SEQUENCE_TEMPLATE';
+"dictionary" return 'DICTIONARY_TEMPLATE';
+{identifier} return 'IDENTIFIER';
+<<EOF>> return 'END_OF_FILE';
+
+/lex
+
+%%
+
+file
+ : module_children END_OF_FILE
+ { $$ = {};
+ for (var child_id in $module_children) {
+ var child = $module_children[child_id];
+ $$[child.name] = child.body;
+ }
+ return $$;
+ }
+ ;
+
+module_children
+ : module module_children
+ { $module_children.unshift($1);
+ $$ = $module_children;
+ }
+ | qualified_interface module_children
+ { $module_children.unshift($1);
+ $$ = $module_children;
+ }
+ | type_definition module_children
+ -> $module_children;
+ | /* end of sequence */
+ -> [];
+ ;
+
+module
+ : MODULE_KEYWORD IDENTIFIER LEFT_BRACE module_children RIGHT_BRACE SEMICOLON
+ { $$ = {name: $IDENTIFIER, body: {}};
+ for (var child_id in $module_children) {
+ var child = $module_children[child_id];
+ $$.body[child.name] = child.body;
+ };
+ }
+ ;
+
+qualified_interface
+ /* We currently don't care about metadata. Ignored. */
+ : interface
+ | metadata_block interface
+ -> $interface;
+ ;
+
+interface
+ : INTERFACE_KEYWORD IDENTIFIER LEFT_BRACE operations RIGHT_BRACE SEMICOLON
+ { $$ = {name: $IDENTIFIER, body: {operations: {}}};
+ for (var operation_index in $operations) {
+ var operation = $operations[operation_index];
+
+ // TODO: Duplicate detection in $$.operations.
+ $$.body.operations[operation.name] = {arguments: $operation.arguments, returns: $operation.returns};
+ }
+ }
+ ;
+
+operations
+ : qualified_operation operations
+ { $operations.unshift($qualified_operation);
+ $$ = $operations;
+ }
+ | /* end of sequence */
+ -> [];
+ ;
+
+qualified_operation
+ /* We thus far don't care about idempotence. Ignored. */
+ : operation
+ | IDEMPOTENT_KEYWORD operation
+ -> $operation;
+
+ /* We thus far don't care about metadata. Ignored. */
+ | metadata_block operation
+ -> $operation;
+ | metadata_block IDEMPOTENT_KEYWORD operation
+ -> $operation;
+ ;
+
+operation
+ : typename IDENTIFIER LEFT_PARENTHESIS argument_declarations RIGHT_PARENTHESIS SEMICOLON
+ -> {name: $IDENTIFIER, arguments: $argument_declarations, returns: {result_type: $typename, output_parameters: []}};
+ ;
+
+type_definition
+ : SEQUENCE_TEMPLATE LEFT_ALLIGATOR typename RIGHT_ALLIGATOR IDENTIFIER SEMICOLON
+ { if ($IDENTIFIER in yy.type_definitions) {
+ $$ = 'wtf sequence';
+ // Error?
+ } else {
+ yy.type_definitions[$IDENTIFIER] = yy.SequenceType($typename);
+ $$ = yy.type_definitions[$IDENTIFIER]; // Unnecessary?
+ }
+ }
+ | DICTIONARY_TEMPLATE LEFT_ALLIGATOR typename COMMA typename RIGHT_ALLIGATOR IDENTIFIER SEMICOLON
+ { if ($IDENTIFIER in yy.type_definitions) {
+ $$ = 'wtf dictionary';
+ // Error?
+ } else {
+ yy.type_definitions[$IDENTIFIER] = yy.DictionaryType($typename1, $typename2);
+ $$ = yy.type_definitions[$IDENTIFIER]; // Unnecessary?
+ }
+ }
+ ;
+
+metadata_block
+ : LEFT_BRACKET metadata RIGHT_BRACKET
+ -> $metadata;
+ ;
+
+metadata
+ : metadatum COMMA metadata
+ -> $metadatum;
+ | metadatum
+ ;
+
+metadatum
+ : QUOTE IDENTIFIER QUOTE
+ -> $IDENTIFIER;
+ ;
+
+argument_declarations
+ : argument_declaration argument_declarations
+ { $argument_declarations.unshift($argument_declaration);
+ $$ = $argument_declarations;
+ }
+ | /* end of sequence */
+ -> [];
+ ;
+
+argument_declaration
+ : typename IDENTIFIER
+ -> {type: $typename, name: $IDENTIFIER}
+ | metadata_block typename IDENTIFIER
+ -> {type: $typename, name: $IDENTIFIER}
+ ;
+
+typename
+ : TYPE_BOOL
+ | TYPE_STRING
+ | TYPE_INT
+ | TYPE_VOID
+ | user_defined_typename
+ -> $1;
+ ;
+
+user_defined_typename
+ : IDENTIFIER
+ { if ($IDENTIFIER in yy.type_definitions) {
+ $$ = yy.type_definitions[$IDENTIFIER];
+ } else {
+ // ERROR?!?!?
+ $$ = 'wtf type is ' + $IDENTIFIER;
+ }
+ }
+ ;
View
@@ -0,0 +1,8 @@
+sequence<string> StrSeq;
+
+interface Object {
+ idempotent void ice_ping ();
+ idempotent bool ice_isA (string typeID);
+ idempotent string ice_id ();
+ idempotent StrSeq ice_ids ();
+};
View
@@ -0,0 +1,32 @@
+var SequenceType = function(element_type) {
+ if (this instanceof SequenceType) {
+ this.element_type = element_type;
+ } else {
+ return new SequenceType(element_type);
+ }
+};
+exports.SequenceType = SequenceType;
+
+
+var DictionaryType = function(key_type, value_type) {
+ if (this instanceof DictionaryType) {
+ this.key_type = key_type;
+ this.value_type = value_type;
+ } else {
+ return new DictionaryType(key_type, value_type);
+ }
+};
+exports.DictionaryType = DictionaryType;
+
+
+var Interface = function(name, operations) {
+ if (this instanceof Interface) {
+
+ } else {
+ return new Interface(name, operations);
+ }
+};
+exports.Interface = Interface;
+
+
+exports.type_definitions = {};

0 comments on commit 73b93a2

Please sign in to comment.