RelAst.parser 4.65 KB
Newer Older
Johannes Mey's avatar
Johannes Mey committed
1
Program goal =
2
3
4
  /* empty */              {: return new Program(); :}
  | type_decl.t goal.p     {: p.getTypeDeclList().insertChild(t, 0); return p; :}
  | relation.r goal.p      {: p.getRelationList().insertChild(r, 0); return p; :}
Johannes Mey's avatar
Johannes Mey committed
5
  ;
Niklas Fors's avatar
Niklas Fors committed
6
7

TypeDecl type_decl =
Johannes Mey's avatar
Johannes Mey committed
8
  ID type_decl_super.s components_opt.c SCOL
9
10
11
12
13
14
15
16
    {:
      TypeDecl result = new TypeDecl();
      result.setID(ID);
      result.setAbstract(false);
      result.setSuperOpt(s);
      result.setComponentList(c);
      return result;
    :}
Johannes Mey's avatar
Johannes Mey committed
17
  | ABSTRACT ID type_decl_super.s components_opt.c SCOL
18
19
20
21
22
23
24
25
      {:
        TypeDecl result = new TypeDecl();
        result.setID(ID);
        result.setAbstract(true);
        result.setSuperOpt(s);
        result.setComponentList(c);
        return result;
      :}
Johannes Mey's avatar
Johannes Mey committed
26
  ;
Niklas Fors's avatar
Niklas Fors committed
27
28

Opt type_decl_super =
Johannes Mey's avatar
Johannes Mey committed
29
30
31
  /* empty */ {: return new Opt(); :}
  | COL s_type_use.u {: return new Opt(u); :}
  ;
32
33

SimpleTypeUse s_type_use =
Johannes Mey's avatar
Johannes Mey committed
34
35
  ID {: return new SimpleTypeUse(ID); :}
  ;
Niklas Fors's avatar
Niklas Fors committed
36

37
38
39
40
41
ArrayList inner_type_use
    = ID
    | inner_type_use DOT ID
    ;

Niklas Fors's avatar
Niklas Fors committed
42
TypeUse type_use =
43
44
  parameterized_type_use.p    {: return p; :}
  | inner_type_use.p            {: return new SimpleTypeUse((String)p.stream().map( x -> ((Symbol)x).value.toString()).collect(java.util.stream.Collectors.joining("."))); :}
Johannes Mey's avatar
Johannes Mey committed
45
  ;
46
ParameterizedTypeUse parameterized_type_use =
47
  inner_type_use.i LT type_use_list.l GT      {: return new ParameterizedTypeUse((String)i.stream().map( x -> ((Symbol)x).value.toString()).collect(java.util.stream.Collectors.joining(".")), l); :}
Johannes Mey's avatar
Johannes Mey committed
48
  ;
49
List type_use_list =
50
51
  type_use.u                              {: return new List().add(u); :}
  | type_use_list.l COMMA type_use.u      {: return l.add(u); :}
Johannes Mey's avatar
Johannes Mey committed
52
  ;
Niklas Fors's avatar
Niklas Fors committed
53
54

List components_opt =
55
56
  /* empty */               {: return new List(); :}
  | ASSIGN components.l     {: return l; :}
Johannes Mey's avatar
Johannes Mey committed
57
  ;
Niklas Fors's avatar
Niklas Fors committed
58
59

List components =
60
61
  /* empty */                   {: return new List(); :}
  | components.l component.c    {: return l.add(c); :}
Johannes Mey's avatar
Johannes Mey committed
62
  ;
Niklas Fors's avatar
Niklas Fors committed
63
64

Component component =
65
66
    ID COL s_type_use.u                       {: return new NormalComponent(ID, u); :}
  | s_type_use.u                              {: return new NormalComponent(u.getID(), u); :}
Johannes Mey's avatar
Johannes Mey committed
67
  // List
68
69
  | ID COL s_type_use.u STAR                  {: return new ListComponent(ID, u); :}
  | s_type_use.u STAR                         {: return new ListComponent(u.getID(), u); :}
Johannes Mey's avatar
Johannes Mey committed
70
  // Opt
71
72
  | LBRACKET ID COL s_type_use.u RBRACKET     {: return new OptComponent(ID, u); :}
  | LBRACKET s_type_use.u RBRACKET            {: return new OptComponent(u.getID(), u); :}
Johannes Mey's avatar
Johannes Mey committed
73
  // NTA list
74
75
  | SLASH ID COL s_type_use.u STAR SLASH      {: return new NTAListComponent(ID, u); :}
  | SLASH s_type_use.u STAR SLASH             {: return new NTAListComponent(u.getID(), u); :}
Johannes Mey's avatar
Johannes Mey committed
76
77
78
  // NTA opt
  | SLASH LBRACKET ID COL s_type_use.u RBRACKET SLASH {: return new NTAOptComponent(ID, u); :}
  | SLASH LBRACKET s_type_use.u RBRACKET SLASH {: return new NTAOptComponent(u.getID(), u); :}
Johannes Mey's avatar
Johannes Mey committed
79
  // NTA
80
81
  | SLASH ID COL s_type_use.u SLASH           {: return new NTAComponent(ID, u); :}
  | SLASH s_type_use.u SLASH                  {: return new NTAComponent(u.getID(), u); :}
Johannes Mey's avatar
Johannes Mey committed
82
  // NTA Token (same as NTA)
83
  | SLASH LT ID COL s_type_use.u GT SLASH     {: return new NTATokenComponent(ID, u); :}
84
  | SLASH LT ID GT SLASH                      {: return new NTATokenComponent(ID, new SimpleTypeUse("String")); :}
Johannes Mey's avatar
Johannes Mey committed
85
  // Token
86
87
  | LT ID COL type_use.u GT                   {: return new TokenComponent(ID, u); :}
  | LT ID GT                                  {: return new TokenComponent(ID, new SimpleTypeUse("String")); :}
Johannes Mey's avatar
Johannes Mey committed
88
  ;
Niklas Fors's avatar
Niklas Fors committed
89
90

Relation relation =
Johannes Mey's avatar
Johannes Mey committed
91
  RELATION relation_comp.l direction relation_comp.r SCOL
92
93
   {:
      Relation result = new Relation();
94
95
96
97
98
99
100
101
102
      if (direction instanceof LeftDirection) {
        result.setLeft(r);
        result.setDirection(new RightDirection());
        result.setRight(l);
      } else {
        result.setLeft(l);
        result.setDirection(direction);
        result.setRight(r);
      }
103
104
      return result;
   :}
Johannes Mey's avatar
Johannes Mey committed
105
  ;
Niklas Fors's avatar
Niklas Fors committed
106

Niklas Fors's avatar
Niklas Fors committed
107
RelationComponent relation_comp =
Johannes Mey's avatar
Johannes Mey committed
108
  // One
109
110
  s_type_use.u DOT ID                       {: return new OneRelationComponent(ID, u); :}
  | s_type_use.u                            {: return new OneRelationComponent("", u); :}
Johannes Mey's avatar
Johannes Mey committed
111
  // Optional
112
113
  | s_type_use.u DOT ID QUESTION_MARK       {: return new OptionalRelationComponent(ID, u); :}
  | s_type_use.u QUESTION_MARK              {: return new OptionalRelationComponent("", u); :}
Johannes Mey's avatar
Johannes Mey committed
114
  // Many
115
116
  | s_type_use.u DOT ID STAR                {: return new ManyRelationComponent(ID, u); :}
  | s_type_use.u STAR                       {: return new ManyRelationComponent("", u); :}
Johannes Mey's avatar
Johannes Mey committed
117
  ;
Niklas Fors's avatar
Niklas Fors committed
118
119

Direction direction =
Johannes Mey's avatar
Johannes Mey committed
120
  RIGHT {: return new RightDirection(); :}
121
  | LEFT {: return new LeftDirection(); :}
Johannes Mey's avatar
Johannes Mey committed
122
123
  | BIDIRECTIONAL {: return new Bidirectional(); :}
  ;