Autolev.g4 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. grammar Autolev;
  2. prog: stat+;
  3. stat: varDecl
  4. | functionCall
  5. | codeCommands
  6. | massDecl
  7. | inertiaDecl
  8. | assignment
  9. | settings
  10. ;
  11. assignment: vec equals expr #vecAssign
  12. | ID '[' index ']' equals expr #indexAssign
  13. | ID diff? equals expr #regularAssign;
  14. equals: ('='|'+='|'-='|':='|'*='|'/='|'^=');
  15. index: expr (',' expr)* ;
  16. diff: ('\'')+;
  17. functionCall: ID '(' (expr (',' expr)*)? ')'
  18. | (Mass|Inertia) '(' (ID (',' ID)*)? ')';
  19. varDecl: varType varDecl2 (',' varDecl2)*;
  20. varType: Newtonian|Frames|Bodies|Particles|Points|Constants
  21. | Specifieds|Imaginary|Variables ('\'')*|MotionVariables ('\'')*;
  22. varDecl2: ID ('{' INT ',' INT '}')? (('{' INT ':' INT (',' INT ':' INT)* '}'))? ('{' INT '}')? ('+'|'-')? ('\'')* ('=' expr)?;
  23. ranges: ('{' INT ':' INT (',' INT ':' INT)* '}');
  24. massDecl: Mass massDecl2 (',' massDecl2)*;
  25. massDecl2: ID '=' expr;
  26. inertiaDecl: Inertia ID ('(' ID ')')? (',' expr)+;
  27. matrix: '[' expr ((','|';') expr)* ']';
  28. matrixInOutput: (ID (ID '=' (FLOAT|INT)?))|FLOAT|INT;
  29. codeCommands: units
  30. | inputs
  31. | outputs
  32. | codegen
  33. | commands;
  34. settings: ID (EXP|ID|FLOAT|INT)?;
  35. units: UnitSystem ID (',' ID)*;
  36. inputs: Input inputs2 (',' inputs2)*;
  37. id_diff: ID diff?;
  38. inputs2: id_diff '=' expr expr?;
  39. outputs: Output outputs2 (',' outputs2)*;
  40. outputs2: expr expr?;
  41. codegen: ID functionCall ('['matrixInOutput (',' matrixInOutput)*']')? ID'.'ID;
  42. commands: Save ID'.'ID
  43. | Encode ID (',' ID)*;
  44. vec: ID ('>')+
  45. | '0>'
  46. | '1>>';
  47. expr: expr '^'<assoc=right> expr # Exponent
  48. | expr ('*'|'/') expr # MulDiv
  49. | expr ('+'|'-') expr # AddSub
  50. | EXP # exp
  51. | '-' expr # negativeOne
  52. | FLOAT # float
  53. | INT # int
  54. | ID('\'')* # id
  55. | vec # VectorOrDyadic
  56. | ID '['expr (',' expr)* ']' # Indexing
  57. | functionCall # function
  58. | matrix # matrices
  59. | '(' expr ')' # parens
  60. | expr '=' expr # idEqualsExpr
  61. | expr ':' expr # colon
  62. | ID? ranges ('\'')* # rangess
  63. ;
  64. // These are to take care of the case insensitivity of Autolev.
  65. Mass: ('M'|'m')('A'|'a')('S'|'s')('S'|'s');
  66. Inertia: ('I'|'i')('N'|'n')('E'|'e')('R'|'r')('T'|'t')('I'|'i')('A'|'a');
  67. Input: ('I'|'i')('N'|'n')('P'|'p')('U'|'u')('T'|'t')('S'|'s')?;
  68. Output: ('O'|'o')('U'|'u')('T'|'t')('P'|'p')('U'|'u')('T'|'t');
  69. Save: ('S'|'s')('A'|'a')('V'|'v')('E'|'e');
  70. UnitSystem: ('U'|'u')('N'|'n')('I'|'i')('T'|'t')('S'|'s')('Y'|'y')('S'|'s')('T'|'t')('E'|'e')('M'|'m');
  71. Encode: ('E'|'e')('N'|'n')('C'|'c')('O'|'o')('D'|'d')('E'|'e');
  72. Newtonian: ('N'|'n')('E'|'e')('W'|'w')('T'|'t')('O'|'o')('N'|'n')('I'|'i')('A'|'a')('N'|'n');
  73. Frames: ('F'|'f')('R'|'r')('A'|'a')('M'|'m')('E'|'e')('S'|'s')?;
  74. Bodies: ('B'|'b')('O'|'o')('D'|'d')('I'|'i')('E'|'e')('S'|'s')?;
  75. Particles: ('P'|'p')('A'|'a')('R'|'r')('T'|'t')('I'|'i')('C'|'c')('L'|'l')('E'|'e')('S'|'s')?;
  76. Points: ('P'|'p')('O'|'o')('I'|'i')('N'|'n')('T'|'t')('S'|'s')?;
  77. Constants: ('C'|'c')('O'|'o')('N'|'n')('S'|'s')('T'|'t')('A'|'a')('N'|'n')('T'|'t')('S'|'s')?;
  78. Specifieds: ('S'|'s')('P'|'p')('E'|'e')('C'|'c')('I'|'i')('F'|'f')('I'|'i')('E'|'e')('D'|'d')('S'|'s')?;
  79. Imaginary: ('I'|'i')('M'|'m')('A'|'a')('G'|'g')('I'|'i')('N'|'n')('A'|'a')('R'|'r')('Y'|'y');
  80. Variables: ('V'|'v')('A'|'a')('R'|'r')('I'|'i')('A'|'a')('B'|'b')('L'|'l')('E'|'e')('S'|'s')?;
  81. MotionVariables: ('M'|'m')('O'|'o')('T'|'t')('I'|'i')('O'|'o')('N'|'n')('V'|'v')('A'|'a')('R'|'r')('I'|'i')('A'|'a')('B'|'b')('L'|'l')('E'|'e')('S'|'s')?;
  82. fragment DIFF: ('\'')*;
  83. fragment DIGIT: [0-9];
  84. INT: [0-9]+ ; // match integers
  85. FLOAT: DIGIT+ '.' DIGIT*
  86. | '.' DIGIT+;
  87. EXP: FLOAT 'E' INT
  88. | FLOAT 'E' '-' INT;
  89. LINE_COMMENT : '%' .*? '\r'? '\n' -> skip ;
  90. ID: [a-zA-Z][a-zA-Z0-9_]*;
  91. WS: [ \t\r\n&]+ -> skip ; // toss out whitespace