Permalink
Browse files

Actions working for subsuper_record, simple_entity_instance, and comp…

…lex_entity_instance.
  • Loading branch information...
1 parent a985a89 commit 49064e147ea253cd39141686db671e2abc4e040f @colomon committed Oct 20, 2012
Showing with 67 additions and 11 deletions.
  1. +11 −10 lib/ISO_10303_21/Actions.pm
  2. +2 −1 lib/ISO_10303_21/Grammar.pm
  3. +54 −0 t/05-actions.t
View
21 lib/ISO_10303_21/Actions.pm
@@ -7,13 +7,15 @@ class ISO_10303_21::Record {
}
class ISO_10303_21::Actions {
+ has %.entities;
+
method entity_instance_name($/) { make [~$/] }
- method parameter($/) { make $/.values[0].ast }
- method omitted_parameter($/) { make [] }
- method untyped_parameter($/) { make $/.values[0].ast // [] }
- method typed_parameter($/) { make $<parameter>.ast }
- method list_of_parameters($/) { make @($<parameter>.ast.map(*.list).Array }
- method parameter_list($/) { make @($<parameter>.ast.map(*.list).Array }
+ method parameter($/) { make $/.values[0].ast }
+ method omitted_parameter($/) { make [] }
+ method untyped_parameter($/) { make $/.values[0].ast // [] }
+ method typed_parameter($/) { make $<parameter>.ast }
+ method list_of_parameters($/) { make @($<parameter>.ast.map(*.list).Array }
+ method parameter_list($/) { make @($<parameter>.ast.map(*.list).Array }
method simple_record($/) {
# bit awkward, but this way works in both Rakudo and Niecza
@@ -27,8 +29,7 @@ class ISO_10303_21::Actions {
:entity_instances([]));
}
}
- # method subsuper_record($/) { '(' <simple_record>+ ')' }
- # method simple_entity_instance($/) { <entity_instance_name> '=' <simple_record> ';' }
- # method complex_entity_instance($/) { <entity_instance_name> '=' <subsuper_record> ';' }
- # method entity_instance($/) { <simple_entity_instance> | <complex_entity_instance> }
+ method subsuper_record($/) { make $<simple_record>».ast }
+ method simple_entity_instance($/) { %.entities{$<entity_instance_name>} = $<simple_record>.ast }
+ method complex_entity_instance($/) { %.entities{$<entity_instance_name>} = $<subsuper_record>.ast }
}
View
3 lib/ISO_10303_21/Grammar.pm
@@ -42,7 +42,8 @@ grammar ISO_10303_21::Grammar
token parameter { <typed_parameter> | <untyped_parameter> | <omitted_parameter> }
token omitted_parameter { '*' }
- token untyped_parameter { '$' | <real> | <integer> | <string>
+ token default_parameter { '$' }
+ token untyped_parameter { <default_parameter> | <real> | <integer> | <string>
| <entity_instance_name> | <enumeration> | <binary> | <list_of_parameters> }
rule typed_parameter { <keyword> '(' <parameter> ')' }
rule list_of_parameters { '(' [ <parameter> ]* % [ ',' ] ')' }
View
54 t/05-actions.t
@@ -91,4 +91,58 @@ isa_ok $match.ast.keyword, Str, "with a Str keyword";
is $match.ast.keyword, "LEAF", "'LEAF'";
is +$match.ast.entity_instances, 0, "and the entity_instances has length 0";
+$match = ISO_10303_21::Grammar.parse("(LEAF('Blah', #42) BRANCH(#1949))",
+ :rule<subsuper_record>,
+ :actions(ISO_10303_21::Actions.new));
+isa_ok $match, Match, "<subsuper_record> matches LEAF('Blah', #42) BRANCH(#1949)";
+ok $match, "<subsuper_record> matches LEAF('Blah', #42) BRANCH(#1949)";
+is +$match.ast, 2, "Got two records";
+isa_ok $match.ast[0], ISO_10303_21::Record, "and the ast[0] is ISO_10303_21::Record";
+isa_ok $match.ast[1], ISO_10303_21::Record, "and the ast[1] is ISO_10303_21::Record";
+is $match.ast[0].keyword, "LEAF", "'LEAF'";
+is $match.ast[1].keyword, "BRANCH", "'BRANCH'";
+
+$match = ISO_10303_21::Grammar.parse("PRODUCT_RELATED_PRODUCT_CATEGORY('detail',\$,(#8));",
+ :rule<simple_record>,
+ :actions(ISO_10303_21::Actions.new));
+isa_ok $match, Match, "<simple_record> matches PRODUCT_RELATED_PRODUCT_CATEGORY('detail',\$,(#8));";
+ok $match, "<simple_record> matches PRODUCT_RELATED_PRODUCT_CATEGORY('detail',\$,(#8));";
+isa_ok $match.ast, ISO_10303_21::Record, "and the ast is ISO_10303_21::Record";
+isa_ok $match.ast.keyword, Str, "with a Str keyword";
+is $match.ast.keyword, "PRODUCT_RELATED_PRODUCT_CATEGORY", "'PRODUCT_RELATED_PRODUCT_CATEGORY'";
+is +$match.ast.entity_instances, 1, "and the entity_instances has length 1";
+
+{
+ my $file = ISO_10303_21::Actions.new;
+ my $step = "DATA; #24=ORGANIZATION('','None','None'); #25=PERSON_AND_ORGANIZATION(#23,#24); ENDSEC;";
+ $match = ISO_10303_21::Grammar.parse($step,
+ :rule<data_section>,
+ :actions($file));
+ isa_ok $match, Match, "<data_section> matches STEP file snippet";
+ ok $match, "<data_section> matches ";
+ is +$file.entities, 2, "Got two entities";
+ is $file.entities{'#24'}.keyword, "ORGANIZATION", '#24 is an ORGANIZATION';
+ is $file.entities{'#25'}.keyword, "PERSON_AND_ORGANIZATION", '#25 is an PERSON_AND_ORGANIZATION';
+}
+
+{
+ my $file = ISO_10303_21::Actions.new;
+ my $file-data = slurp("t/CAx/as1-id-203.stp");
+ $file-data .= subst(/"/*" .*? "*/"/, " ", :global);
+
+ my $match = ISO_10303_21::Grammar.parse($file-data,
+ :rule<exchange_file>,
+ :actions($file));
+ isa_ok $match, Match, "Grammar.<exchange_file> returns a Match for $file...";
+ ok $match, "... and it matches";
+
+ is $file.entities{'#24'}.keyword, "ORGANIZATION", '#24 is an ORGANIZATION';
+ is $file.entities{'#25'}.keyword, "PERSON_AND_ORGANIZATION", '#25 is an PERSON_AND_ORGANIZATION';
+ # (LENGTH_UNIT()NAMED_UNIT(*)SI_UNIT(.MILLI.,.METRE.));
+ is +$file.entities{'#116'}, 3, '#116 has 3 bits';
+ is $file.entities{'#116'}>>.keyword.sort.join(" "),
+ "LENGTH_UNIT NAMED_UNIT SI_UNIT",
+ "and they have correct keywords";
+}
+
done;

0 comments on commit 49064e1

Please sign in to comment.