Skip to content

Commit

Permalink
more processing
Browse files Browse the repository at this point in the history
  • Loading branch information
adrpo committed Oct 13, 2019
1 parent 1043477 commit 2c60af5
Show file tree
Hide file tree
Showing 7 changed files with 60 additions and 58 deletions.
22 changes: 10 additions & 12 deletions grammar/B22_class_definition.g
Expand Up @@ -7,8 +7,8 @@ class_definition :

class_prefixes :
'partial'?
( 'class' | 'model' | 'operator'? 'record' | 'block' | 'expandable'? 'connector' |
'type' | 'package' | ( 'pure' | 'impure' )? 'operator'? 'function' | 'operator' )
( 'class' | 'model' | 'operator'? 'record' | 'block' | 'expandable'? 'connector' | 'type' |
'package' | ( 'pure' | 'impure' )? 'operator'? 'function' | 'operator' )
;

class_specifier :
Expand All @@ -34,16 +34,14 @@ der_class_specifier :
IDENT '=' 'der' '(' type_specifier ',' IDENT ( ',' IDENT )* ')' comment
;

base_prefix
: ( 'input' | 'output' )?
base_prefix :
( 'input' | 'output' )?
;

enum_list :
enumeration_literal ( ',' enumeration_literal )*
enum_list : enumeration_literal ( ',' enumeration_literal )*
;

enumeration_literal :
IDENT comment
enumeration_literal : IDENT comment
;

composition :
Expand All @@ -53,7 +51,7 @@ composition :
equation_section |
algorithm_section
)*
( 'external' language_specification
( 'external' language_specification?
external_function_call? annotation_comment? ';' )?
( annotation_comment ';' )?
;
Expand All @@ -77,13 +75,13 @@ element :
'redeclare'?
'final'?
'inner'? 'outer'?
( ( class_definition | component_clause) |
( class_definition | component_clause |
'replaceable' ( class_definition | component_clause )
(constraining_clause comment)? )
( constraining_clause comment )? )
;

import_clause :
'import' ( IDENT '=' name | name ('.' ( '*' | '{' import_list '}' ) | '.*')? ) comment
'import' ( IDENT '=' name | name ( '.' ( '*' | '{' import_list '}' ) | '.*' )? ) comment
;

import_list :
Expand Down
1 change: 0 additions & 1 deletion grammar/B23_extends.g
Expand Up @@ -7,4 +7,3 @@ extends_clause :
constraining_clause :
'constrainedby' type_specifier class_modification?
;

1 change: 0 additions & 1 deletion grammar/B24_component_clause.g
Expand Up @@ -24,4 +24,3 @@ condition_attribute :
declaration :
IDENT array_subscripts? modification?
;

27 changes: 13 additions & 14 deletions grammar/B25_modification.g
Expand Up @@ -10,26 +10,26 @@ class_modification :
'(' argument_list? ')'
;

argument_list
: argument ( ',' argument )*
argument_list :
argument ( ',' argument )*
;

argument
: element_modification_or_replaceable
argument :
element_modification_or_replaceable
| element_redeclaration
;

element_modification_or_replaceable
: 'each'? 'final'? ( element_modification | element_replaceable )
element_modification_or_replaceable :
'each'? 'final'? ( element_modification | element_replaceable )
;

element_modification
: name modification? string_comment
element_modification :
name modification? string_comment
;

element_redeclaration :
'redeclare' 'each'? 'final'?
( ( short_class_definition | component_clause1 ) | element_replaceable )
( short_class_definition | component_clause1 | element_replaceable )
;

element_replaceable :
Expand All @@ -41,11 +41,10 @@ component_clause1 :
type_prefix type_specifier component_declaration1
;

component_declaration1
: declaration comment
component_declaration1 :
declaration comment
;

short_class_definition
: class_prefixes short_class_specifier
short_class_definition :
class_prefixes short_class_specifier
;

6 changes: 2 additions & 4 deletions grammar/B26_equations.g
Expand Up @@ -84,17 +84,15 @@ when_equation :
'when' expression 'then'
( equation ';' )*
( 'elsewhen' expression 'then'
( equation ';' )*
)*
( equation ';' )* )*
'end' 'when'
;

when_statement :
'when' expression 'then'
( statement ';' )*
( 'elsewhen' expression 'then'
( statement ';' )*
)*
( statement ';' )* )*
'end' 'when'
;

Expand Down
31 changes: 13 additions & 18 deletions grammar/B27_expressions.g
Expand Up @@ -63,12 +63,10 @@ primary :
| 'end'
;

type_specifier :
'.'? name
type_specifier : '.'? name
;

name :
IDENT ( '.' IDENT )*
name : IDENT ( '.' IDENT )*
;

component_reference :
Expand All @@ -79,32 +77,29 @@ function_call_args :
'(' function_arguments? ')'
;

function_arguments
: expression ( ',' function_arguments_non_first | 'for' for_indices )?
| function_partial_application (',' function_arguments_non_first)?
function_arguments :
expression ( ',' function_arguments_non_first | 'for' for_indices )?
| function_partial_application ( ',' function_arguments_non_first )?
| named_arguments
;

function_arguments_non_first
: ( function_argument (',' function_arguments_non_first)?
| named_arguments
)
function_arguments_non_first :
function_argument ( ',' function_arguments_non_first )?
| named_arguments
;

array_arguments :
expression ( ( ',' expression )* | 'for' for_indices ) // CHANGE: repetition instead of a new rule
;

named_arguments :
named_argument ( ',' named_argument )* // CHANGE: repetition
named_arguments : named_argument ( ',' named_argument )* // CHANGE: repetition
;

named_argument :
IDENT '=' function_argument
named_argument : IDENT '=' function_argument
;

function_argument
: function_partial_application
function_argument :
function_partial_application
| expression
;

Expand All @@ -117,7 +112,7 @@ output_expression_list :
;

expression_list :
expression ( ',' expression? )*
expression ( ',' expression )*
;

array_subscripts :
Expand Down
30 changes: 22 additions & 8 deletions grammar/Makefile
Expand Up @@ -12,24 +12,38 @@ all_tex: $(ALL_TEX)
mv $@.tmp $@

../chapters/%.tex: %.g
@echo off
# delete the grammar declaration line
sed '/lexer grammar/d' $< > $@.tmp1
sed '/parser grammar/d' < $@.tmp1 > $@.tmp2
# delete start_rule
sed '/start_rule/d' < $@.tmp2 > $@.tmp1
# change _ to - in rule names
sed -e "s/\([^']\)_\([^']\)/\1-\2/g" < $@.tmp1 > $@.tmp2
# remove lines containing only ;
sed '/^[[:space:]]*;$$/d' < $@.tmp2 > $@.tmp1
sed '/^[[:space:]]*;$$/d' < $@.tmp1 > $@.tmp2
# change 'keyword' to keyword
sed -e "s/'\([[:alpha:]]\+\)'/\1/g" < $@.tmp1 > $@.tmp2

echo '% generated file! DO NOT EDIT! Edit the ../grammar/.g file instead' > $@.tmp
sed -e "s/'\([[:alpha:]]\+\)'/\1/g" < $@.tmp2 > $@.tmp1
# ( ABC )* -> { ABC }
perl -0777 -pi -e 's/\(([^()]*)\)\*/\{$$1\}/gms' $@.tmp1
perl -0777 -pi -e 's/\(([^()]*)\)\*/\{$$1\}/gms' $@.tmp1
# ABC? -> [ ABC ]
perl -0777 -pi -e 's/(\w+)\?/\[ $$1 \]/gms' $@.tmp1
# '?' -> "?"
sed -e "s/'\(.\{1,2\}\)'/\"\1\"/g" < $@.tmp1 > $@.tmp2
# "ABC"? -> [ "ABC" ]
perl -0777 -pi -e 's/(\".\")\?/\[ $$1 \]/gms' $@.tmp2
# ( ABC )? -> [ ABC ]
perl -0777 -pi -e 's/\(([^()]*)\)\?/\[$$1\]/gms' $@.tmp2
perl -0777 -pi -e 's/\(([^()]*)\)\?/\[$$1\]/gms' $@.tmp2
# ( (ABC) )? -> [ (ABC) ]
perl -0777 -pi -e 's/\(([^(]*\([^()]*\)[^)]*)\)\?/\[$$1\]/gms' $@.tmp2
# change _ to - in rule names
sed -e "s/\([^']\)_\([^']\)/\1-\2/g" < $@.tmp2 > $@.tmp1
# echo '% generated file! DO NOT EDIT! Edit the ../grammar/.g file instead' > $@.tmp
echo '\begin{lstlisting}[language=grammar]' >> $@.tmp
cat $@.tmp2 >> $@.tmp
cat $@.tmp1 >> $@.tmp
echo '\end{lstlisting}' >> $@.tmp
mv $@.tmp $@
rm -f $@.tmp1 $@.tmp2
rm -f $@.tmp1 $@.tmp2 $@.tmp1.bak $@.tmp2.bak

clean:
rm -rf *Listener*.java ModelicaLexer.java ModelicaParser.java Modelica_*.java *.tokens *.interp *.g4 *.class $(ALL_TEX)

0 comments on commit 2c60af5

Please sign in to comment.