|
| 1 | +# Interscript ISC grammar — parsanol grammar language (PG). |
| 2 | +# |
| 3 | +# Line-oriented port of lib/interscript/isc/grammar/concerns/*.rb. |
| 4 | +# The capture tree must reproduce the Ruby-DSL parser's tree exactly: |
| 5 | +# - repetitions are ABNF prefixes (1*x, *x) |
| 6 | +# - negation is !x, positive lookahead is &x |
| 7 | +# - captures at use sites (rule_ref as name) nest inner captures |
| 8 | +# the same way parslet .as does |
| 9 | +# |
| 10 | +# Consumed by the interscript gem (artifact parse) and parsanol npm. |
| 11 | + |
| 12 | +grammar InterscriptIsc version "0.1.0" { |
| 13 | + |
| 14 | + # -- lexical primitives ------------------------------------------------- |
| 15 | + |
| 16 | + chr = %x00-10FFFF |
| 17 | + space_char = %x20 / %x09 / %x0A / %x0B / %x0C / %x0D |
| 18 | + newline = %x0D.0A / %x0A / %x0D |
| 19 | + line_comment = "#" *(!(newline) chr) |
| 20 | + whitespace = 1*(space_char / line_comment) |
| 21 | + ws = *(space_char / line_comment) |
| 22 | + inline_space = 1*(%x20 / %x09) |
| 23 | + inline_ws = *(inline_space / line_comment) |
| 24 | + hexd = %x30-39 / %x41-46 / %x61-66 |
| 25 | + digit = %x30-39 |
| 26 | + |
| 27 | + ident_first = %x41-5A / %x61-7A / %x5F |
| 28 | + ident_rest = %x41-5A / %x61-7A / %x30-39 / %x5F |
| 29 | + identifier = (ident_first 0*ident_rest) as identifier |
| 30 | + |
| 31 | + keyword = "parallel" / "sequence" / "stage" / "compose" / "separate" / "system" / "metadata" / "aliases" / "tests" / "notes" / "description" / "any_character" / "authority" / "dependency" / "run" / "sub" / "before" / "after" / "not_before" / "not_after" / "any" / "none" / "boundary" / "line_start" / "line_end" / "word_boundary" / "downcase" / "upcase" / "title_case" / "capture" / "maybe" / "some" / "ref" / "name" |
| 32 | + |
| 33 | + # -- string literals ---------------------------------------------------- |
| 34 | + |
| 35 | + esc_nl = ("n") as newline |
| 36 | + esc_cr = ("r") as carriage_return |
| 37 | + esc_tab = ("t") as tab |
| 38 | + esc_dq = "\"" as dquote |
| 39 | + esc_bs = "\\" as backslash |
| 40 | + esc_uni4 = "u" (4hexd) as unicode |
| 41 | + esc_uni8 = "U" (8hexd) as unicode |
| 42 | + escape_sequence = "\\" (esc_nl / esc_cr / esc_tab / esc_dq / esc_bs / esc_uni4 / esc_uni8) |
| 43 | + |
| 44 | + raw_run = 1*(!("\\" / "\"") chr) |
| 45 | + dq_parts = *(escape_sequence / (raw_run as run)) |
| 46 | + double_quoted = "\"" (dq_parts as string) "\"" |
| 47 | + sq_body = *(!"'" chr) |
| 48 | + single_quoted = "'" (sq_body as string) "'" |
| 49 | + quoted_string = double_quoted / single_quoted |
| 50 | + |
| 51 | + arrow = [whitespace] "->" [whitespace] |
| 52 | + comma_ws = "," [whitespace] |
| 53 | + |
| 54 | + # -- metadata ----------------------------------------------------------- |
| 55 | + |
| 56 | + md_authority = "authority" whitespace ((quoted_string) as authority) |
| 57 | + md_source_spelling = "source_spelling" whitespace ((quoted_string) as source_spelling) |
| 58 | + md_target_spelling = "target_spelling" whitespace ((quoted_string) as target_spelling) |
| 59 | + md_identifying = "identifying" whitespace ((quoted_string) as identifying) |
| 60 | + md_name = "name" whitespace ((quoted_string) as name) |
| 61 | + md_spec_value = quoted_string as specification |
| 62 | + md_specification = "specification" whitespace ((md_spec_value *(comma_ws md_spec_value)) as specification) |
| 63 | + md_description = "description" [whitespace] "{" [ws] (raw_text as description) [ws] "}" |
| 64 | + md_system_status = "system_status" whitespace (("current" / "former" / "inactive") as system_status) |
| 65 | + md_code_status = "code_status" whitespace (("preferred" / "proposed" / "deprecated") as code_status) |
| 66 | + |
| 67 | + relation_type = "supersedes" / "superseded_by" / "based_on" / "basis_for" / "alias_of" / "adopted_from" / "related_to" |
| 68 | + relation_note = whitespace "note" whitespace (quoted_string as note) |
| 69 | + relation_item = [whitespace] (relation_type as type) whitespace (quoted_string as system) [(relation_note)] [whitespace] |
| 70 | + md_relations = "relations" [whitespace] "{" [ws] ((*(relation_item)) as relations) [ws] "}" |
| 71 | + |
| 72 | + note_item = [whitespace] "note" [whitespace] (quoted_string as note) [whitespace] |
| 73 | + md_notes = "notes" [whitespace] "{" [ws] ((*(note_item)) as notes) [ws] "}" |
| 74 | + |
| 75 | + md_provenance_value = quoted_string as provenance |
| 76 | + md_provenance = "provenance" [whitespace] ((md_provenance_value *(comma_ws md_provenance_value)) as provenance) |
| 77 | + |
| 78 | + md_empty_field = &(newline) |
| 79 | + md_raw_field_value = 1*(!%x0A !"}" !"{" chr) |
| 80 | + md_field_value = quoted_string / (md_raw_field_value as raw) |
| 81 | + md_field_block = "{" [ws] (raw_text as field_block) [ws] "}" |
| 82 | + md_generic_field = (identifier as field_name) inline_ws ((md_field_value as field_value) / md_field_block / md_empty_field) |
| 83 | + |
| 84 | + metadata_field = [whitespace] (md_description / md_relations / md_system_status / md_code_status / md_specification / md_notes / md_provenance / md_generic_field) [whitespace] |
| 85 | + md_items = *(metadata_field) |
| 86 | + metadata_block = "metadata" [whitespace] "{" [ws] (md_items as metadata) [ws] "}" |
| 87 | + |
| 88 | + raw_text = *("\\" chr / (!"}" chr)) |
| 89 | + |
| 90 | + # -- aliases ------------------------------------------------------------ |
| 91 | + |
| 92 | + alias_decl = [whitespace] (identifier as name) [whitespace] "=" [whitespace] (item as value) [whitespace] |
| 93 | + alias_items = *(alias_decl) |
| 94 | + aliases_block = "aliases" [whitespace] "{" [ws] (alias_items as aliases) [ws] "}" |
| 95 | + |
| 96 | + # -- dependencies ------------------------------------------------------- |
| 97 | + |
| 98 | + dep_alias = whitespace "as" whitespace (identifier as alias) |
| 99 | + dependency_decl = "dependency" whitespace (quoted_string as target) [(dep_alias)] |
| 100 | + |
| 101 | + # -- item expressions --------------------------------------------------- |
| 102 | + |
| 103 | + zw_primitive = (("boundary") / ("line_start") / ("line_end") / ("word_boundary") / ("space") / ("non_boundary")) as primitive |
| 104 | + fn_lit = (("upcase") / ("downcase") / ("title_case") / ("reverse") / ("strip") / ("swapcase")) as function |
| 105 | + any_char_lit = ("any_character") as any_char |
| 106 | + none_lit = ("none") as none |
| 107 | + |
| 108 | + digit_cap = digit as digit |
| 109 | + capture_reference_body = "ref" "(" [ws] digit_cap [ws] ")" |
| 110 | + capture_reference = capture_reference_body as ref |
| 111 | + |
| 112 | + item_ref = identifier "." (identifier as qualified_name) |
| 113 | + alias_reference = (!(keyword) identifier [("." (identifier as qualified_name))] !"{" ) as alias |
| 114 | + |
| 115 | + item_seq = item_atom |
| 116 | + item_sep = ([ws] "+" [ws]) / whitespace |
| 117 | + item_cont = &item_atom |
| 118 | + |
| 119 | + range_arg = (quoted_string as lo) [ws] ".." [ws] (quoted_string as hi) |
| 120 | + list_item = item |
| 121 | + list_items = (list_item *((comma_ws / whitespace) list_item)) |
| 122 | + set_arg = ((quoted_string as single)) / ("[" [ws] ((list_item *((comma_ws / whitespace) list_item)) as list) [ws] "]") |
| 123 | + alias_arg = (!zw_primitive !(keyword) identifier) as alias_ref |
| 124 | + any_arg = range_arg / set_arg / alias_arg |
| 125 | + any_ctor = "any" "(" [ws] (any_arg as any) [ws] ")" |
| 126 | + capture_ctor = "capture" "(" [ws] (item as capture_inner) [ws] ")" |
| 127 | + maybe_ctor = "maybe" "(" [ws] (item as maybe_inner) [ws] ")" |
| 128 | + some_ctor = "some" "(" [ws] (item as some_inner) [ws] ")" |
| 129 | + |
| 130 | + item_atom_start = "\"" / "'" / ident_first |
| 131 | + cont_stop = "to" / "before" / "after" / "not_before" / "not_after" / "}" |
| 132 | + item_atom = quoted_string / none_lit / zw_primitive / any_char_lit / any_ctor / capture_ctor / maybe_ctor / some_ctor / fn_lit / capture_reference / alias_reference |
| 133 | + item = (item_atom 0*(item_sep !cont_stop &item_atom_start item_atom)) as concatenation |
| 134 | + |
| 135 | + # -- rules and stages --------------------------------------------------- |
| 136 | + |
| 137 | + constraint_after = "after" whitespace (item as after) |
| 138 | + constraint_before = "before" whitespace (item as before) |
| 139 | + constraint_not_before = "not_before" whitespace (item as not_before) |
| 140 | + constraint_not_after = "not_after" whitespace (item as not_after) |
| 141 | + constraint = constraint_after / constraint_before / constraint_not_before / constraint_not_after |
| 142 | + constraints = *(whitespace constraint) |
| 143 | + |
| 144 | + compact_rule = "sub" whitespace (item_atom as from) whitespace (item_atom as to) ((*(whitespace constraint)) as constraints) |
| 145 | + |
| 146 | + block_rule = "sub" [ws] "{" [ws] "from" whitespace (item as from) [ws] "to" whitespace (item as to) ((*(whitespace constraint)) as constraints) [ws] "}" |
| 147 | + |
| 148 | + rule = block_rule / compact_rule |
| 149 | + |
| 150 | + comment_item = (("#" *(!%x0A chr)) as comment) / ((!(keyword) identifier) as noop) |
| 151 | + rule_line = [ws] (rule / comment_item) [ws] |
| 152 | + rule_items = *(rule_line) |
| 153 | + sequence_block = "sequence" [ws] "{" [ws] (rule_items as sequence) [ws] "}" |
| 154 | + parallel_block = "parallel" [ws] "{" [ws] (rule_items as parallel) [ws] "}" |
| 155 | + |
| 156 | + separator_part = whitespace "separator" whitespace (item_atom as separator) |
| 157 | + separate_directive = ("separate") as separate [(separator_part)] |
| 158 | + case_directive = (("downcase") / ("upcase")) as case |
| 159 | + case_kwargs_part = whitespace (kwarg_list as case_kwargs) |
| 160 | + title_case_directive = ("title_case") as case [(case_kwargs_part)] |
| 161 | + string_case_directive = case_directive / title_case_directive |
| 162 | + compose_directive = (("compose") / ("decompose")) as compose |
| 163 | + kwarg = (identifier as kwarg_name) [ws] ":" [ws] ((quoted_string) as kwarg_value) |
| 164 | + kwarg_item = (kwarg) as kwarg |
| 165 | + kwarg_list = (kwarg_item *(comma_ws kwarg_item)) |
| 166 | + funcall_kwargs_part = whitespace (kwarg_list as funcall_kwargs) |
| 167 | + rababa_directive = ("rababa") as funcall_name [(funcall_kwargs_part)] |
| 168 | + run_dep = "map." (identifier as dep) ".stage." (identifier as stage) |
| 169 | + run_stage_only = ("stage." (identifier as stage)) as run_stage_only |
| 170 | + run_rule = "run" whitespace ((run_dep) / run_stage_only) |
| 171 | + |
| 172 | + bare_rule = (rule) as bare_rule |
| 173 | + stage_item = [ws] (sequence_block / parallel_block / run_rule / separate_directive / string_case_directive / compose_directive / rababa_directive / bare_rule / comment_item) [ws] |
| 174 | + stage_items = *(stage_item) |
| 175 | + stage_name_paren = "(" (identifier as stage_name) ")" |
| 176 | + stage_name_ws = whitespace (identifier as stage_name) |
| 177 | + stage_block = "stage" (stage_name_paren / stage_name_ws) [ws] "{" [ws] (stage_items as stage) [ws] "}" |
| 178 | + |
| 179 | + # -- tests --------------------------------------------------------------- |
| 180 | + |
| 181 | + test_note = whitespace "note" whitespace (quoted_string as note) |
| 182 | + test_line = [whitespace] (quoted_string as input) arrow (quoted_string as expected) [(test_note)] [whitespace] |
| 183 | + test_items = *(test_line) |
| 184 | + tests_block = "tests" [whitespace] "{" [ws] (test_items as tests) [ws] "}" |
| 185 | + |
| 186 | + # -- system -------------------------------------------------------------- |
| 187 | + |
| 188 | + block_item = [whitespace] (metadata_block / aliases_block / tests_block / stage_block / dependency_decl) [whitespace] |
| 189 | + body_items = *(block_item) |
| 190 | + system_block = "system" whitespace ((quoted_string) as system_code) [whitespace] "{" [ws] ((*(block_item)) as body) [ws] "}" [whitespace] |
| 191 | + isc_source = [whitespace] ((system_block) as system) [whitespace] |
| 192 | + |
| 193 | + entry isc: isc_source |
| 194 | +} |
0 commit comments