(* ===================================================================================================== UJN grammar: the text of a UI Journey Notation diagram, from @startujn to @endujn. Written for spec revision 23 (proposal/ujn-language-v23.md): section 22 made exact, and checked line by line against the PlantUML module (UjnParser.java, UjnValues.java) and the PlantUML code that reads a file before UJN does (diagram splitting, comments, the preprocessor). Tested by ujn_grammar.py and run_tests.py. --------------------------------------------------------------------------------------------------- The notation of this file --------------------------------------------------------------------------------------------------- name = expression ; a rule. Names are letters, digits, "-" and "_". The first rule is the start. "text" a terminal, matched exactly (case matters). Inside it, \" is a quote, \\ a backslash, \n a line end and \t a tab. /regex/ a lexical class: a Python regular expression, matched at the current place (one match, as Python's re finds it). Inside it, \/ is a slash, and <> stands for the regex of the rule NAME (a rule made of one /regex/). [ e ] e, or nothing. { e } e, zero or more times. ( e ) a group. e1 e2 e1, then e2. e1 | e2 e1 or e2. comments from a left parenthesis and a star to the next star and right parenthesis. A choice and a repetition try every way, not only the first that fits: the grammar is an ordinary context-free grammar over its terminals and lexical classes, read with backtracking. A file is valid when the start rule, `file`, covers all of it. UPPER-CASE names are lexical classes (as in section 22); lower-case names are the syntax. Names that start with raw- are the text the module reads as a value whatever its key; `ujn_grammar.py --strict` drops them, so that every value must take one of the forms of section 5. --------------------------------------------------------------------------------------------------- How a file is read --------------------------------------------------------------------------------------------------- Line ends are "\n". As PlantUML does, the checker reads CR LF and a CR alone as a line end, and drops a byte order mark at the start of the file. The grammar covers files made of printable characters, tabs and line ends. Other control characters (U+0000 to U+001F except tab, line feed and carriage return) and the characters Java treats as line ends inside a line (U+0085, U+2028, U+2029), and U+E103 (a PlantUML line break), are out of scope: the rule SCOPE refuses a file that holds one. Also out of scope, because they are PlantUML features with no place in a UJN file: - @pause, @unpause and @append lines (PlantUML drops or adds lines with them); - a line where @start follows only punctuation, such as `' @startujn` or `# @startujn` (PlantUML starts a diagram there, and removes that punctuation from the lines after it); - inside block comments and preprocessor blocks, and in text outside a diagram, the grammar follows a line that ends with one backslash only as far as noted at each rule. --------------------------------------------------------------------------------------------------- What the grammar covers, and what it leaves to the module --------------------------------------------------------------------------------------------------- The grammar follows the module's parser line by line, and the PlantUML steps before it: how a file is cut into diagrams, which lines are comments, which lines the preprocessor takes. Everything the module only warns about (21) is valid here: an unknown key, a key that does not apply, a value of the wrong kind, a missing title. Which value form a key takes depends on the key (section 5 and the key tables of each section), so `value` lists every form of section 5 and ends with `raw-value`. Left to the module, because they need more than a grammar: - two nodes with the same handle, display id, unique id or friendly id, and an arrow to a handle that no statement declares (syntax errors in the module, 21); - what the preprocessor does with the lines it takes. They are skipped here, not run: an !if without its !endif (or an !endif, !else or !endwhile alone), a failed !assert, a missing !include or theme, a Python error, or a function without !return is not caught (pinned in differ/, named module-only-preprocessor-...); - two module bugs, where the module crashes: a number too large for it (`view_start: 99999999999`), and a block comment or preprocessor block that is never closed after the journey line alone (pinned in differ/, named module-only-crash-...). Stricter than the module on purpose (each case is pinned by a file in differ/, named module-reads-...): - a guard that starts with a double quote and is not one closed string (`"x" or "y`, `"abc`): the module checks only the first character (a module bug); - a guard that starts with [ and has no ] (`: [Save`), or that starts with [ after a trigger; and, after a trigger whose double quote is not closed, a guard that holds a double quote other than as one closed string; - a line that ends with one backslash: PlantUML joins it to the next line before UJN reads it; - braces that do not pair, on a line that ends with }: a line that closes a block (`title: a } b }`), a block that opens and closes on one line (`view a { title: x }{{auto}}`), or a line that opens a block; and a value where a brace pair holds a square bracket that does not pair (`title: {[}`); - lines the preprocessor writes: the grammar reads a preprocessor line as a line to skip, and every other line as written, so a statement that a macro, a procedure, an !foreach, the branch of an !if that is not taken, or a !{...} line writes is not seen; - a /'''name'''/ comment inside a word (PlantUML removes it anywhere; the grammar only between tokens). ===================================================================================================== *) (* ---------------------------------------------------------------- the file and its diagrams (2, 3) *) (* As PlantUML does, a file may hold text outside its diagrams, other PlantUML diagrams, and more than one UJN diagram. A diagram runs from a line that starts with @start (or \start) to the next line that starts with @end (or \end); one that a new @start line interrupts is dropped. *) file = SCOPE { outside } diagram { outside | diagram } ; SCOPE = /(?![\s\S]*[\x00-\x08\x0B\x0C\x0E-\x1F\x85\u2028\u2029\uE103])/ ; outside = OUTSIDE-LINE | OTHER-DIAGRAM | DROPPED-START ; OUTSIDE-LINE = /(?!<>)(?:[^\n]*(?>)[^\n]*\n)*[ \t]*[@\\]end[^\n]*(?:\n|(?![\s\S]))/ ; DROPPED-START = /<>[^\n]*\n(?:(?!<>)[^\n]*\n)*(?:(?=<>)|(?!<>)[^\n]*(?![\s\S]))/ ; (* a start line with no end line before the next start line or the end of the file *) START = /[ \t]*[@\\]start[^\n]/ ; DIRECTIVE = /[ \t]*[@\\](?:start[^\n]|end)/ ; (* a start line or an end line *) LINE = /[^\n]*(?>)<>)*---/ NL ; (* a PlantUML header right after the start line: lines between two lines that are exactly --- *) (* ---------------------------------------------------------------- lines to skip (2) *) skip = blank-line | comment-line | block-comment | preproc ; blank-line = _ NL ; comment-line = /[ \t]*'[^\n]*/ NL ; (* first non-blank character is ' *) block-comment = /[ \t]*\/'[^\n]*'\/[ \t]*\n/ (* a line that starts with /' and ends with '/ *) | /[ \t]*\/'\/[ \t]*\n/ (* a line that holds only /'/ is blank *) | /[ \t]*\/'(?!\/)(?![^\n]*'\/)<>(?:(?!<>)(?![^\n]*'\/[ \t]*\n)<>)*(?:(?!<>)[^\n]*'\/[ \t]*\n|(?=[ \t]*[@\\]end))/ ; (* /' on a line with no '/, then every line up to the first that ends with '/; with none, the rest of the diagram (PlantUML drops it) *) (* The preprocessor takes these lines; the grammar skips them without running them. *) preproc = /[ \t]*(?:<>[ \t]*)?!(?:<>)<>(?:(?!<>)(?!<>)<>)*(?:<><>|(?=[ \t]*[@\\]end))/ (* a procedure, a function or a definelong, up to the first !endprocedure, !endfunction or !enddefinelong (any of the three); with none, the rest of the diagram *) | /[ \t]*!python[ \t]*\n(?:(?!<>)(?![ \t]*!endpy[ \t]*\n)<>)*[ \t]*!endpy[ \t]*\n/ | /[ \t]*(?:<>[ \t]*)?!/ pp-line /[^\n]*/ NL | /[ \t]*!exit[ \t]*\n(?:(?!<>)<>)*(?=[ \t]*[@\\]end)/ ; (* !exit: PlantUML drops the rest of the diagram *) PP-OPENER = /(?:(?:unquoted|final)[ \t])*procedure[ \t]+\$?[^\W\d]\w*[ \t]*\(|(?:(?:unquoted|final)[ \t])*function[ \t]+\$?[^\W\d]\w*[ \t]*\((?![^\n]*\)[ \t]*!?return\b)|definelong[ \t]+[^\W\d]/ ; PP-CLOSER = /[ \t]*!end[ \t]*(?:function|definelong|procedure)\b/ ; (* The one-line directives, after the ! (TLineType and PythonLine in PlantUML). Any other line that starts with ! (`!pragma`, `!!x`, `!{...}`, `! if`, `!define` alone, `!return` outside a function) is handed to UJN, which refuses it. *) pp-line = /[ \t]*[(\[0-9@]/ (* a Python statement or decorator *) | /[ \t]*(?!<>)[^\W\d]/ (* a Python statement: !x = 1, !print(...) *) | /define[ \t]+[^\W\d]/ | /(?:ifdef|undef|ifndef|assert|if|else|elseif|endif|while|endwhile|foreach|endfor|theme|includedef|import|startsub|endsub|includesub|log|dump_memory|option)\b/ | /include[ \t]*(?:url|_many|_once)?\b/ | /end[ \t]*(?:function|definelong|procedure)\b/ | /[ \t]*(?:(?:local|global)[ \t]+\$?[^\W\d]\w*|\$[^\W\d]\w*)[ \t]*\??=/ | /[ \t]*[^\W\d]\w*[ \t]*\?=/ | /(?:(?:unquoted|final)[ \t])*function[ \t]+\$?[^\W\d]\w*[ \t]*\((?=[^\n]*\)[ \t]*!?return\b)/ ; (* a function and its !return on one line *) PP-WORD = /(?:python|endpy|define|definelong|enddefinelong|undef|ifdef|ifndef|if|elseif|else|endif|while|endwhile|foreach|endfor|function|procedure|unquoted|final|end|endfunction|endprocedure|return|include|includeurl|include_many|include_once|include_sprites|includedef|includesub|import|startsub|endsub|theme|log|dump_memory|option|assert|local|pragma|assume|exit)(?!\w)/ ; (* ---------------------------------------------------------------- the start and end of a content line *) (* PlantUML removes block comments before UJN reads a line, without looking at quotes: - a line that starts with /' and ends with '/ is a comment (block-comment above); - a line that starts with /' : the comment runs to the first '/ (it may share the ', as in /'/); - a line that ends with '/ : the comment runs from the last /' (TCUT); - a /'''name'''/ comment (letters, digits, _ and -) is removed from the rest of the line. Then UJN removes a comment that starts with a ' after a blank, outside double quotes. (2) *) lead = /[ \t]*/ [ LEAD-COMMENT _ ] ; LEAD-COMMENT = /\/'(?![^\n]*'\/[ \t]*\n)(?:\/|(?!\/)(?:(?!'\/)[^\n])*'\/)/ ; line-end = ( WS "'" COMMENT-TEXT | _ ) [ TAIL-COMMENT ] NL ; quoted-end = _ [ TAIL-COMMENT ] NL ; (* a line that ends inside double quotes: a ' is text there, not a comment *) COMMENT-TEXT = /(?:(?!<>)[^\n])*/ ; TAIL-COMMENT = /(?=<>)[^\n]*/ ; TCUT = /\/'(?:(?!\/')[^\n])*(?<='\/)[ \t]*\n/ ; (* from the last /' of a line that ends with '/ to the line end. No text class below runs into it, so such a line can only end with TAIL-COMMENT. *) (* ---------------------------------------------------------------- the journey line (3) *) journey-line = lead "journey" WS JOURNEY-ID WS STRING line-end ; journey-entry = lead /(?!journey[ \t])/ KEY _ ":" [ _ journey-value ] line-end ; (* one entry per line, and the colon is required. A line that starts with journey and a blank is read as a journey line, so the key is not journey there. *) journey-value = value | raw-line-value ; (* ---------------------------------------------------------------- statements (4, 6, 10.2, 13, 19.1) *) top-statement = lead top-head block-part ; statement = lead ( top-head | node-head | arrow-head ) block-part | lead ( node-head-q | arrow-head-q ) quoted-end [ later-block ] ; (* a header that ends inside double quotes: its block, if any, opens on a later line *) top-head = "increment" | "render" | COLOR-BLOCK ; COLOR-BLOCK = /(?:view|actor|system|action|handoff|data|start|end|ref|arrow)_color(?:_[a-z][a-z0-9-]*)?/ ; (* _color, _color_, arrow_color, arrow_color_; a suffix that is not a type of the kind is a warning *) node-head = KIND WS HANDLE [ _ "[" DISPLAY-ID "]" ] ; node-head-q = KIND WS HANDLE _ "[" DISPLAY-ID BRACKET-QUOTE-TAIL "]" ; KIND = "view" | "actor" | "system" | "action" | "handoff" | "data" | "start" | "end" | "ref" ; arrow-head = HANDLE WS TOKEN WS HANDLE [ _ ":" [ _ label ] ] ; arrow-head-q = HANDLE WS TOKEN WS HANDLE _ ":" _ label-q ; TOKEN = "-->" | "->" | "-x->" | "..>" | ".>" | ".x.>" | "-" DIR "->" | "-" DIR "-x->" | "." DIR ".>" | "." DIR ".x.>" ; (* 6 + 4 x 8 = 38 tokens *) DIR = "right" | "left" | "up" | "down" | "r" | "l" | "u" | "d" ; label = trigger [ _ guard ] | guard ; (* a trigger, a guard, or both (13.4) *) label-q = "[" BRACKET-TEXT BRACKET-QUOTE-TAIL "]" [ _ ( GUARD-STRING-Q | GUARD-TEXT-Q ) ] | [ trigger _ ] GUARD-TEXT GUARD-QUOTE-TAIL ; (* a trigger or a guard with a double quote that is not closed *) trigger = "[" BRACKET-TEXT "]" ; guard = STRING | GUARD-TEXT ; (* quote a guard that holds ; { } or ' *) (* ---------------------------------------------------------------- blocks (4.4) *) (* A statement's block opens with { on its line, or alone at the start of a later line, with only lines to skip between. Entries are key: value, one per line or separated by ;. A key on its own means true. The block closes on a line that ends with } and holds more } than {, outside double quotes; a block that opens and closes on one line holds as many { as }. *) block-part = _ block | line-end [ later-block ] ; later-block = { skip } lead block ; block = "{" _ [ entries _ ] "}" line-end (* on one line *) | "{" [ _ open-entries ] line-end (* over several lines *) { skip | block-line } closing-line ; block-line = lead open-entries line-end ; closing-line = lead [ entries _ ] "}" line-end ; (* Entries are split at each ; outside double quotes and square brackets. A [ or ] without its partner keeps the ; after it in the value (raw-open-bracket). *) entries = [ semis _ ] entry { semis _ entry } [ semis ] | semis (* empty entries are dropped *) | [ semis _ ] { entry semis _ } KEY _ ":" _ [ raw-value ] raw-open-bracket ; (* On a line that leaves the block open, two more things are text: a double quote that is not closed (the rest of the line is then the value), and a brace without its partner, when the line does not end with }. *) open-entries = [ semis _ ] open-entry { semis _ open-entry } [ semis ] | semis | [ semis _ ] { open-entry semis _ } KEY _ ":" _ [ raw-open-value ] ( raw-open-bracket [ raw-quote-tail ] | raw-quote-tail ) | raw-free-entries /(?>)[^ \t\n;,"'{}\[\]])+/ ; (* kind:reference (9.2) *) handle = HANDLE ; (* one declared in the journey *) text = STRING | BARE-TEXT ; (* What the module reads as a value, whatever the key: the rest of the entry. Braces pair up on the line; square brackets pair up, or pair the other way round (] ... [), and a ; inside them is text; a ' after a blank starts a comment. A value outside the forms above is a warning (21), never a syntax error. *) raw-value = raw-piece { raw-piece } ; raw-piece = raw-flat | raw-braces | raw-square | raw-reversed ; raw-flat = /[^ \t\n"'\/;{}\[\]]+/ | SLASH | APOSTROPHE | INNER-BLANKS | STRING ; raw-braces = "{" { raw-flat | raw-braces | raw-square } "}" ; raw-square = "[" { raw-flat | raw-braces | raw-square | ";" } "]" ; raw-reversed = "]" { raw-flat | raw-flat-braces | ";" | raw-reversed } "[" ; raw-flat-braces = "{" { raw-flat | raw-flat-braces } "}" ; raw-open-bracket = raw-open-square | raw-open-reversed ; raw-open-square = "[" { raw-flat | raw-braces | raw-square | ";" } [ raw-open-square ] ; raw-open-reversed = "]" { raw-flat | raw-flat-braces | ";" | raw-reversed } [ raw-open-reversed ] ; raw-quote-tail = /"(?:(?!<>)(?:[^"\\\n]|\\[^\n]))*/ ; (* a double quote that is not closed, and the rest of the line *) (* On a line that leaves the block open and does not end with }, braces are plain text. *) raw-free-entries = [ semis _ ] raw-free-entry { semis _ raw-free-entry } [ semis ] | [ semis _ ] { raw-free-entry semis _ } KEY _ ":" _ [ raw-free-value ] ( raw-free-open-bracket [ raw-quote-tail ] | raw-quote-tail ) ; raw-free-entry = KEY [ _ ":" [ _ raw-free-value ] ] ; raw-free-value = raw-free-piece { raw-free-piece } ; raw-free-piece = raw-free-flat | raw-free-square | raw-free-reversed ; raw-free-flat = raw-flat | "{" | "}" ; raw-free-square = "[" { raw-free-flat | raw-free-square | ";" } "]" ; raw-free-reversed = "]" { raw-free-flat | ";" | raw-free-reversed } "[" ; raw-free-open-bracket = raw-free-open-square | raw-free-open-reversed ; raw-free-open-square = "[" { raw-free-flat | raw-free-square | ";" } [ raw-free-open-square ] ; raw-free-open-reversed = "]" { raw-free-flat | ";" | raw-free-reversed } [ raw-free-open-reversed ] ; (* A journey entry takes the rest of its line: ; braces and brackets are plain characters there. *) raw-line-value = { raw-line-piece } ( raw-line-piece | raw-quote-tail ) ; raw-line-piece = /[^ \t\n"'\/]+/ | SLASH | APOSTROPHE | INNER-BLANKS | STRING ; (* ---------------------------------------------------------------- lexical classes (22) *) HANDLE = /(?!(?:view|actor|system|journey|increment|action|handoff|data|start|end|ref)(?![A-Za-z0-9_]))[A-Za-z][A-Za-z0-9_]*/ ; (* letters, digits and _, starting with a letter; not a keyword (4.2) *) KEY = /[a-z][a-z0-9_]*/ ; JOURNEY-ID = /[A-Za-z0-9][A-Za-z0-9._-]*/ ; STRING = /"(?:(?!<>)(?:[^"\\\n]|\\[^\n]))*"/ ; (* \" is a quote; any other backslash pair is read as in a PlantUML label: \n, \\, \t *) (* Three pieces that every unquoted text below shares, so that a comment is never read as text: *) APOSTROPHE = /(?>)\// ; (* a / that does not start TCUT *) (* The text of a display id and of a trigger: anything except ] and a line end. A { outside double quotes would open the block, and a ' after a blank starts a comment. A part in double quotes holds no ]. A double quote that is not closed may end the text (BRACKET-QUOTE-TAIL); the line then ends inside quotes. *) BRACKET-TEXT = { /[^\]\n{"'\/ \t]+/ | SLASH | APOSTROPHE | INNER-BLANKS | /"(?:(?!<>)(?:[^"\\\n\]]|\\[^\n\]]))*"/ } ; BRACKET-QUOTE-TAIL = /"(?:(?!<>)(?:[^"\\\]\n]|\\[^\]\n]))*\\?/ ; DISPLAY-ID = BRACKET-TEXT ; (* A guard without quotes around it: no ; { } or ', and it does not start with [ or ". A part in double quotes inside it holds no ; } or ' either. *) GUARD-TEXT = ( /[^ \t\n"'\/;{}\[]/ | SLASH ) { /[^ \t\n"'\/;{}]+/ | SLASH | INNER-BLANKS | /"(?:[^"\\\n;}']|\\[^\n;}'])*"/ } ; GUARD-QUOTE-TAIL = /"(?:(?!\/')(?:[^"\\\n;}']|\\[^\n;}']))*/ ; (* a double quote that is not closed, and the rest of the guard: no ; } or ' *) (* A guard after a trigger whose double quote is not closed: the guard starts inside quotes. *) GUARD-TEXT-Q = /[^\[" \t\n;}'](?:(?!\/')[^"\n;}'])*/ ; GUARD-STRING-Q = /"[^"\\\n{};']*"/ ; (* Text without quotes (5): no ; { } ' or ", and square brackets in pairs. *) BARE-TEXT = ( /[^ \t\n"'\/;{}\[\]]+/ | SLASH | /\[[^\]\n"'{};]*\]/ ) { /[^ \t\n"'\/;{}\[\]]+/ | SLASH | /\[[^\]\n"'{};]*\]/ | INNER-BLANKS } ; (* A list item without quotes: no , [ ] ; { } ' or ". *) ITEM-TEXT = ( /[^ \t\n"'\/;,{}\[\]]+/ | SLASH ) { /[^ \t\n"'\/;,{}\[\]]+/ | SLASH | INNER-BLANKS } ; (* ---------------------------------------------------------------- characters *) _ = { /[ \t]/ | SPECIAL } ; (* blanks, if any *) WS = { SPECIAL } /[ \t]/ _ ; (* one blank or more *) SPECIAL = /(?!<>)\/'''[-A-Za-z0-9_]*'''\// ; (* PlantUML removes it wherever it stands *) NL = /(?