sourcelibs/Parsing/src/Parsing.xtl

1⍝│ Parsing: total parsers for macro authors: a notation in a string in, its pieces out or a refusal, nothing dropped. 2⍝│ Import it with an alias of your choice: "pa:" u_se< "Parsing". 3⍝│ Put libs/Parsing/src on XETAL_PATH ("just path"); the reference is libs/Parsing/docs. 4⍝│ Names with l: are exported; those under h: are private to this file. 5⍝│ 6⍝│ A macro parses a notation written inside a string; the language 7⍝│ outside the string is untouched (X_eTaL macros cannot change how 8⍝│ source is read, MC10). A parser that uses part of its text and drops 9⍝│ the rest gives a silent wrong answer (the Equations library once 10⍝│ did). These parsers each use all of their text or refuse the call: a 11⍝│ group must close at its last character, a reference must end at its 12⍝│ bracket, a chain must have a value beside every operator; and a whole 13⍝│ parse can be written back and compared with its text. Refusing 14⍝│ parsers take the refusal's tag in their left argument: the macro's 15⍝│ error code and the place to report at (left, right or call), as 16⍝│ []R_EJECT takes them ("bad-equation right"), so a refusal reads as 17⍝│ the notation's own error. 18 19"t:" u̲se< "Strings" 20 21⍝━━ Pieces of text 22 23⍝│ t without its blanks: spaces, tabs and newlines. 24⍝│ >> "pa:" u_se< "Parsing" 25⍝│ >> pa:s_queeze "a + b" 26⍝│ a+b 27ˡs̲queeze ← { t → (n̲ot t m̲ember? " \t\n") r̲eplicate t } 28 29⍝│ 1 at each character of t that is one of ops and at the top level: 30⍝│ outside parentheses, brackets and braces. 31⍝│ >> "+-" pa:t_op "a + (b - c)" 32⍝│ 0 0 1 0 0 0 0 0 0 0 0 33ˡt̲op ← { ops t → 34 d ← '+ s̲\ (0 + t m̲ember? "([{") − 0 + t m̲ember? ")]}" 35 (t m̲ember? ops) ∧ d = 0 36} 37 38⍝│ t cut at each character ch at the top level, the pieces trimmed and 39⍝│ boxed; no ch gives the one piece. 40⍝│ >> "," pa:s_plit "a, f(b, c), d" 41⍝│ ┌→──────────────────┐ 42⍝│ │ ┌→┐ ┌→──────┐ ┌→┐ │ 43⍝│ │ │a│ │f(b, c)│ │d│ │ 44⍝│ │ └─┘ └───────┘ └─┘ │ 45⍝│ └∊──────────────────┘ 46ˡs̲plit ← { ch t → 47 cut ← ch ˡt̲op t 48 k ← (1 + '+ s̲\ 0 + cut) × n̲ot cut 49 '{ p → ᵗt̲rim d̲isclose p } m̲ap k p̲artition t 50} 51 52⍝│ The position of the first w in t, or 0. 53⍝│ >> ":=" pa:a_t "i:=0..n" 54⍝│ 2 55ˡa̲t ← { w t → 56 i ← w ᵗf̲ind t 57 0 = t̲ally i ? 0 58 f̲irst i 59} 60 61⍝│ Whether t is one group in the brackets (open, close): it opens with 62⍝│ open and the depth returns to zero once only, at its last character. 63⍝│ >> ("(", ")") pa:c_losed "(a)(b)" 64⍝│ 0 65⍝│ >> ("(", ")") pa:c_losed "(a (b) c)" 66⍝│ 1 67ˡc̲losed ← { (open, close) t → 68 0 = t̲ally t ? 0 = 1 69 n̲ot open m̲atch 1 t̲ake t ? 0 = 1 70 d ← '+ s̲\ (0 + t m̲ember? open) − 0 + t m̲ember? close 71 (1 = '+ r̲/ 0 + d = 0) ∧ 0 = f̲irst -1 t̲ake d 72} 73 74⍝│ Whether t is one parenthesized group. 75⍝│ >> pa:g_roup? "((a) + b)" 76⍝│ 1 77ˡg̲roup? ← { t → ("(", ")") ˡc̲losed t } 78 79⍝│ The text inside a group: without its first and last characters. 80⍝│ >> pa:i_nner "(a + b)" 81⍝│ a + b 82ˡi̲nner ← { t → 1 d̲rop -1 d̲rop t } 83 84⍝━━ Kinds of text 85 86⍝│ Whether t is a lowercase name: a lowercase letter, then letters or digits. 87⍝│ >> pa:n_ame? "x2" 88⍝│ 1 89ˡn̲ame? ← { t → (0 < t̲ally t) ∧ ((f̲irst t) m̲ember? lowers) ∧ '× r̲/ 0 + t m̲ember? lowers c̲at uppers c̲at digits } 90 91⍝│ Whether t is a number: digits, with at most one point between digits. 92⍝│ >> pa:n_umber? "2.5" 93⍝│ 1 94⍝│ >> pa:n_umber? "2." 95⍝│ 0 96ˡn̲umber? ← { t → 97 points ← '+ r̲/ 0 + t m̲ember? "." 98 ok ← (0 < t̲ally t) ∧ '× r̲/ 0 + t m̲ember? digits c̲at "." 99 ok ∧ (points < 2) ∧ ((f̲irst t) m̲ember? digits) ∧ (f̲irst -1 t̲ake t) m̲ember? digits 100} 101 102⍝│ Whether t is a word of name characters: letters, digits, _ and : 103⍝│ (an X_eTaL name with its namespace), as macros write them. 104⍝│ >> pa:w_ord? "nn:r_elu" 105⍝│ 1 106ˡw̲ord? ← { t → (0 < t̲ally t) ∧ '× r̲/ 0 + t m̲ember? lowers c̲at uppers c̲at digits c̲at "_:" } 107 108⍝━━ Refusals 109 110⍝│ Nothing when ok holds; else the call is refused with message under 111⍝│ the tag where (an error code and a place: "bad-text right"), as 112⍝│ []R_EJECT does. 113ˡr̲equire ← { ok (where, message) → 114 ok ? "" 115 where ⎕R̲EJECT message 116} 117 118⍝│ t when every character of it is in allowed; else a refusal naming 119⍝│ the first that is not. 120ˡc̲haracters ← { (where, allowed) t → 121 bad ← (n̲ot t m̲ember? allowed) r̲eplicate t 122 0 < t̲ally bad ? where ⎕R̲EJECT "not a character of this notation: " c̲at (1 t̲ake bad) c̲at " in " c̲at t 123 t 124} 125 126⍝│ A parse written back equals its text without blanks, or the call is 127⍝│ refused: a rule dropped something. 128ˡw̲hole ← { where (text, reprint) → 129 (reprint m̲atch ˡs̲queeze text) ˡr̲equire (where, "the parser did not use all of the text: it read " c̲at reprint c̲at " from " c̲at text) 130} 131 132⍝━━ Parsers 133 134⍝│ A chain cut at its top-level operators ops: (parts, the operators, 135⍝│ whether it opens with a minus); each operator must have a value on 136⍝│ each side (a leading minus negates the first), else a refusal. 137ˡp̲arts ← { (where, ops) t → 138 cut ← ops ˡt̲op t 139 os ← cut r̲eplicate t 140 k ← (1 + '+ s̲\ 0 + cut) × n̲ot cut 141 parts ← '{ p → ᵗt̲rim d̲isclose p } m̲ap k p̲artition t 142 neg ← ("-" m̲atch 1 t̲ake t) ∧ "-" m̲atch 1 t̲ake os 143 rest ← neg ʰp̲ickText (1 d̲rop os, os) 144 _ ← ((t̲ally parts) = 1 + t̲ally rest) ˡr̲equire (where, "an operator needs a value on each side: " c̲at t) 145 _ ← ('× r̲/ 1 c̲at '{ p → 0 < t̲ally d̲isclose p } e̲ach parts) ˡr̲equire (where, "an operator needs a value on each side: " c̲at t) 146 (parts, rest, neg) 147} 148 149⍝│ A binding "word i:=low..high" as (word, index, low, high): the word 150⍝│ one of words, the index a name, the range written with := and ..; 151⍝│ else a refusal, which calls the word what kind says ("an operator 152⍝│ word"). 153ˡb̲inding ← { (where, kind, words) t → 154 s ← ᵗt̲rim t 155 ws ← ᵗw̲ords s 156 _ ← (0 < t̲ally ws) ˡr̲equire (where, "an empty binding: each is " c̲at kind c̲at ", an index, := and a range low..high") 157 word ← d̲isclose f̲irst ws 158 known ← 0 < '+ r̲/ 0 + '{ w → (d̲isclose w) m̲atch word } e̲ach words 159 _ ← known ˡr̲equire (where, "a binding starts with " c̲at kind c̲at " (" c̲at (", " ᵗj̲oin words) c̲at "): " c̲at s) 160 rest ← ᵗt̲rim (t̲ally word) d̲rop s 161 at ← ":=" ˡa̲t rest 162 ix ← ᵗt̲rim (at − 1) t̲ake rest 163 range ← ᵗt̲rim (at + 1) d̲rop rest 164 dots ← ".." ˡa̲t range 165 _ ← ((0 < at) ∧ 0 < dots) ˡr̲equire (where, "a binding is " c̲at kind c̲at ", an index, := and a range low..high, not " c̲at s) 166 _ ← (ˡn̲ame? ix) ˡr̲equire (where, "an index is a lowercase name: " c̲at s) 167 (word, ix, ᵗt̲rim (dots − 1) t̲ake range, ᵗt̲rim (dots + 1) d̲rop range) 168} 169 170⍝│ A function applied, "f(a, b)", as (name, arguments): a name, then 171⍝│ one parenthesized group that ends the text, its arguments cut at the 172⍝│ top-level commas; else a refusal. Whether f is a function of the 173⍝│ notation, and how many arguments it takes, is the caller's to check. 174ˡa̲pplied ← { where t → 175 open ← "(" ˡa̲t t 176 name ← (open − 1) t̲ake t 177 shaped ← (0 < open) ∧ (ˡw̲ord? name) ∧ ˡg̲roup? (open − 1) d̲rop t 178 _ ← shaped ˡr̲equire (where, "a function applied is a name, then its arguments in parentheses that end the text: " c̲at t) 179 (name, "," ˡs̲plit ˡi̲nner (open − 1) d̲rop t) 180} 181 182⍝│ Whether t has the shape of a function applied: a word, then a 183⍝│ parenthesized group that ends the text. 184⍝│ >> pa:a_pplied? "exp(x)" 185⍝│ 1 186⍝│ >> pa:a_pplied? "exp(x) + 1" 187⍝│ 0 188ˡa̲pplied? ← { t → 189 open ← "(" ˡa̲t t 190 0 = open ? 0 = 1 191 (0 < open) ∧ (ˡw̲ord? (open − 1) t̲ake t) ∧ ˡg̲roup? (open − 1) d̲rop t 192} 193 194⍝│ An array reference "A[i,j]" as (name, indices): a word, then its 195⍝│ indices in brackets that end the text, cut at the top-level commas; 196⍝│ else a refusal. 197ˡr̲eference ← { where t → 198 open ← "[" ˡa̲t t 199 name ← ᵗt̲rim (open − 1) t̲ake t 200 _ ← ((0 < open) ∧ ˡw̲ord? name) ˡr̲equire (where, "an array reference is a name, then its indices in brackets: " c̲at t) 201 _ ← (("[", "]") ˡc̲losed (open − 1) d̲rop t) ˡr̲equire (where, "an array reference ends with its closing bracket; nothing follows it: " c̲at t) 202 (name, "," ˡs̲plit ˡi̲nner (open − 1) d̲rop t) 203} 204 205⍝━━ Characters 206 207lowers ← "abcdefghijklmnopqrstuvwxyz" 208uppers ← "ABCDEFGHIJKLMNOPQRSTUVWXYZ" 209digits ← "0123456789" 210 211⍝│ a when c holds, else b (texts, already computed). 212ʰp̲ickText ← { c (a, b) → 213 c ? a 214 b 215}