f73a0fe304bd697702399f40d30ca3994e2635cd
[fleet.git] / src / edu / berkeley / fleet / assembler / fleet.g
1 // The FLEET Assembly Language Grammar
2 // Updated 05-Nov-2006
3
4 // Note that this is the *entire, complete* formal specification of
5 // the grammar.  An equivalent lex+yacc grammar and support code would
6 // be several times as long.
7
8 Comment        = "//" ~[\n\r]!* [\r\n]!
9                | "/*" ~[\n\r]!* "*/"
10 ws             = ([\r\n ] | Comment)!* -> ~[\r\n ]
11
12 s               = ws! Program ws!
13 Program         = Program:: Directive+/ws
14                 | Program:: ("":: Directive+/ws) ws! CodeBagBody
15
16 Statement       = Fiber::                  Source  ":" ("":: Instruction +/ ws)     /ws
17                 | Literal::                int     ":" "sendto" Port       ";" /ws
18                 | ShipSpecificLiteral::    SSL     ":" "sendto" Port       ";" /ws
19                 | CodeBagDescriptor::      CodeBag ":" "sendto" Port       ";" /ws
20                 | NamedCodeBag::           name    ":" "{" CodeBagBody "}"     /ws
21
22 CountField      = Brack::     "[" int "]"
23                 | Paren::     "(" int ")"
24                 | BrackStar:: "[*]"
25                 | ParenStar:: "(*)"
26 Instruction     = Instruction::
27                         ("":: (CountField ws!)?)
28                         ("":: Command +/ (ws! "," ws!) ws! ";"!)
29 Command         = Nop::      "nop"
30                 | Kill::     "kill"
31                 | KillStar:: "kill*"
32                 | Wait::     "wait"
33                 | Discard::  "dismiss"
34                 | Take::     ("take"|"receive")
35                 | SendTo::   "sendto" ws! Port
36                 | DataOutDest::   "send"
37                 | Deliver::  "deliver"
38                 | Ack::      "notify" ws! Port
39
40 Source         = Port
41                | ShipSpecific
42
43 SSL            = ShipSpecificLiteral:: shiptype "." ShipSpecificLiteral
44 Port           = Port::     shipname "." portname
45                | SubPort::  shipname "." portname "." portname
46                |           ^"()"
47
48 CodeBagBody::  = Statement +/ ws
49 CodeBag        = CodeBagRef:: CodeBagName
50                | AnonymousCodeBag:: "{" CodeBagBody "}" /ws
51
52 CodeBagName         = name
53 shipname            = name
54 shiptype            = Name:: [A-Z]    ("":: [A-Za-z0-9\[\]_]**)
55 ShipSpecificLiteral = Name:: [A-Z]    ("":: [A-Z0-9\[\]_]**)
56 portname            = Name:: [a-z]    ("":: [A-Za-z0-9\[\]_]**)
57 name                = Name:: [A-Za-z] ("":: [A-Za-z0-9\[\]_]**)
58 index               = "[" ("":: [0-9]+) "]" | ("":: [0-9]+)
59 int                 = "":: [\-0-9]++
60 ShipSpecific        = ShipSpecific:: "\"" (~[\"])++ "\""
61
62 // the following are not part of the official FLEET syntax and are
63 // specific to Adam's interpreter.
64
65 Directive      =           "#memory" "{" (Memory:: int +/ (ws! "," ws!)) "}" /ws
66                | Import::  "#import" [A-Za-z_.]++ /ws
67                | Include:: "#include" ws! "\"" ~[\"]+ "\""
68                | Ship::    "#ship" shipname ":" ("":: [0-9A-Za-z_.]++) /ws
69                | Expect::  "#expect" int /ws
70                | Skip::    "#skip"
71