librarylibs/Parsing/src/Parsing.xtl

Parsing: total parsers for macro authors: a notation in a string in, its pieces out or a refusal, nothing dropped. Import it with an alias of your choice: "pa:" u_se< "Parsing". Put libs/Parsing/src on XETAL_PATH ("just path"); the reference is libs/Parsing/docs. Names with l: are exported; those under h: are private to this file.

A macro parses a notation written inside a string; the language outside the string is untouched (X_eTaL macros cannot change how source is read, MC10). A parser that uses part of its text and drops the rest gives a silent wrong answer (the Equations library once did). These parsers each use all of their text or refuse the call: a group must close at its last character, a reference must end at its bracket, a chain must have a value beside every operator; and a whole parse can be written back and compared with its text. Refusing parsers take the refusal's tag in their left argument: the macro's error code and the place to report at (left, right or call), as []R_EJECT takes them ("bad-equation right"), so a refusal reads as the notation's own error.

source · imports t: libs/Strings/src/Strings.xtl

Pieces of text

ˡs̲queeze : Char -> Char

function · line 27

t without its blanks: spaces, tabs and newlines.

      "pa:" u̲se< "Parsing"
      ᵖᵃs̲queeze "a + b"
a+b
ˡs̲queeze ← { t → (n̲ot t m̲ember? " \t\n") r̲eplicate t }

ˡt̲op : Truthy a => Char -> Char -> a

function · line 33

1 at each character of t that is one of ops and at the top level: outside parentheses, brackets and braces.

      "+-" ᵖᵃt̲op "a + (b - c)"
0 0 1 0 0 0 0 0 0 0 0
ˡt̲op ← { ops t →
  d ← '+ s̲\ (0 + t m̲ember? "([{") − 0 + t m̲ember? ")]}"
  (t m̲ember? ops) ∧ d = 0
}

ˡs̲plit : Char -> Char -> Box Char

function · line 46

t cut at each character ch at the top level, the pieces trimmed and boxed; no ch gives the one piece.

      "," ᵖᵃs̲plit "a, f(b, c), d"
┌→──────────────────┐
│ ┌→┐ ┌→──────┐ ┌→┐ │
│ │a│ │f(b, c)│ │d│ │
│ └─┘ └───────┘ └─┘ │
└∊──────────────────┘
ˡs̲plit ← { ch t →
  cut ← ch ˡt̲op t
  k ← (1 + '+ s̲\ 0 + cut) × n̲ot cut
  '{ p → ᵗt̲rim d̲isclose p } m̲ap k p̲artition t
}

ˡa̲t : Eq a => a -> a -> Int

function · line 55

The position of the first w in t, or 0.

      ":=" ᵖᵃa̲t "i:=0..n"
2
ˡa̲t ← { w t →
  i ← w ᵗf̲ind t
  0 = t̲ally i ? 0
  f̲irst i
}

ˡc̲losed : (Eq a, Truthy b) => (a, a) -> a -> b

function · line 67

Whether t is one group in the brackets (open, close): it opens with open and the depth returns to zero once only, at its last character.

      ("(", ")") ᵖᵃc̲losed "(a)(b)"
0
      ("(", ")") ᵖᵃc̲losed "(a (b) c)"
1
ˡc̲losed ← { (open, close) t →
  0 = t̲ally t ? 0 = 1
  n̲ot open m̲atch 1 t̲ake t ? 0 = 1
  d ← '+ s̲\ (0 + t m̲ember? open) − 0 + t m̲ember? close
  (1 = '+ r̲/ 0 + d = 0) ∧ 0 = f̲irst -1 t̲ake d
}

ˡg̲roup? : Truthy a => Char -> a

function · line 77

Whether t is one parenthesized group.

      ᵖᵃg̲roup? "((a) + b)"
1
ˡg̲roup? ← { t → ("(", ")") ˡc̲losed t }

ˡi̲nner : a -> a

function · line 82

The text inside a group: without its first and last characters.

      ᵖᵃi̲nner "(a + b)"
a + b
ˡi̲nner ← { t → 1 d̲rop -1 d̲rop t }

Kinds of text

ˡn̲ame? : (Num a, Truthy a) => Char -> a

function · line 89

Whether t is a lowercase name: a lowercase letter, then letters or digits.

      ᵖᵃn̲ame? "x2"
1
ˡ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 }

ˡn̲umber? : Truthy a => Char -> a

function · line 96

Whether t is a number: digits, with at most one point between digits.

      ᵖᵃn̲umber? "2.5"
1
      ᵖᵃn̲umber? "2."
0
ˡn̲umber? ← { t →
  points ← '+ r̲/ 0 + t m̲ember? "."
  ok ← (0 < t̲ally t) ∧ '× r̲/ 0 + t m̲ember? digits c̲at "."
  ok ∧ (points < 2) ∧ ((f̲irst t) m̲ember? digits) ∧ (f̲irst -1 t̲ake t) m̲ember? digits
}

ˡw̲ord? : Truthy a => Char -> a

function · line 106

Whether t is a word of name characters: letters, digits, _ and : (an X_eTaL name with its namespace), as macros write them.

      ᵖᵃw̲ord? "nn:r_elu"
1
ˡw̲ord? ← { t → (0 < t̲ally t) ∧ '× r̲/ 0 + t m̲ember? lowers c̲at uppers c̲at digits c̲at "_:" }

Refusals

ˡr̲equire : (Num a, Truthy a) => a -> (Char, Char) -> Char

function · line 113

Nothing when ok holds; else the call is refused with message under the tag where (an error code and a place: "bad-text right"), as []R_EJECT does.

ˡr̲equire ← { ok (where, message) →
  ok ? ""
  where ⎕R̲EJECT message
}

ˡc̲haracters : (Char, Char) -> Char -> Char

function · line 120

t when every character of it is in allowed; else a refusal naming the first that is not.

ˡc̲haracters ← { (where, allowed) t →
  bad ← (n̲ot t m̲ember? allowed) r̲eplicate t
  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
  t
}

ˡw̲hole : Char -> (Char, Char) -> Char

function · line 128

A parse written back equals its text without blanks, or the call is refused: a rule dropped something.

ˡw̲hole ← { where (text, reprint) →
  (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)
}

Parsers

ˡp̲arts : Truthy a => (Char, Char) -> Char -> (Box Char, Char, a)

function · line 137

A chain cut at its top-level operators ops: (parts, the operators, whether it opens with a minus); each operator must have a value on each side (a leading minus negates the first), else a refusal.

ˡp̲arts ← { (where, ops) t →
  cut ← ops ˡt̲op t
  os ← cut r̲eplicate t
  k ← (1 + '+ s̲\ 0 + cut) × n̲ot cut
  parts ← '{ p → ᵗt̲rim d̲isclose p } m̲ap k p̲artition t
  neg ← ("-" m̲atch 1 t̲ake t) ∧ "-" m̲atch 1 t̲ake os
  rest ← neg ʰp̲ickText (1 d̲rop os, os)
  _ ← ((t̲ally parts) = 1 + t̲ally rest) ˡr̲equire (where, "an operator needs a value on each side: " c̲at t)
  _ ← ('× 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)
  (parts, rest, neg)
}

ˡb̲inding : (Char, Char, Box Char) -> Char -> (Char, Char, Char, Char)

function · line 153

A binding "word i:=low..high" as (word, index, low, high): the word one of words, the index a name, the range written with := and ..; else a refusal, which calls the word what kind says ("an operator word").

ˡb̲inding ← { (where, kind, words) t →
  s ← ᵗt̲rim t
  ws ← ᵗw̲ords s
  _ ← (0 < t̲ally ws) ˡr̲equire (where, "an empty binding: each is " c̲at kind c̲at ", an index, := and a range low..high")
  word ← d̲isclose f̲irst ws
  known ← 0 < '+ r̲/ 0 + '{ w → (d̲isclose w) m̲atch word } e̲ach words
  _ ← known ˡr̲equire (where, "a binding starts with " c̲at kind c̲at " (" c̲at (", " ᵗj̲oin words) c̲at "): " c̲at s)
  rest ← ᵗt̲rim (t̲ally word) d̲rop s
  at ← ":=" ˡa̲t rest
  ix ← ᵗt̲rim (at − 1) t̲ake rest
  range ← ᵗt̲rim (at + 1) d̲rop rest
  dots ← ".." ˡa̲t range
  _ ← ((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)
  _ ← (ˡn̲ame? ix) ˡr̲equire (where, "an index is a lowercase name: " c̲at s)
  (word, ix, ᵗt̲rim (dots − 1) t̲ake range, ᵗt̲rim (dots + 1) d̲rop range)
}

ˡa̲pplied : Char -> Char -> (Char, Box Char)

function · line 174

A function applied, "f(a, b)", as (name, arguments): a name, then one parenthesized group that ends the text, its arguments cut at the top-level commas; else a refusal. Whether f is a function of the notation, and how many arguments it takes, is the caller's to check.

ˡa̲pplied ← { where t →
  open ← "(" ˡa̲t t
  name ← (open − 1) t̲ake t
  shaped ← (0 < open) ∧ (ˡw̲ord? name) ∧ ˡg̲roup? (open − 1) d̲rop t
  _ ← shaped ˡr̲equire (where, "a function applied is a name, then its arguments in parentheses that end the text: " c̲at t)
  (name, "," ˡs̲plit ˡi̲nner (open − 1) d̲rop t)
}

ˡa̲pplied? : Truthy a => Char -> a

function · line 188

Whether t has the shape of a function applied: a word, then a parenthesized group that ends the text.

      ᵖᵃa̲pplied? "exp(x)"
1
      ᵖᵃa̲pplied? "exp(x) + 1"
0
ˡa̲pplied? ← { t →
  open ← "(" ˡa̲t t
  0 = open ? 0 = 1
  (0 < open) ∧ (ˡw̲ord? (open − 1) t̲ake t) ∧ ˡg̲roup? (open − 1) d̲rop t
}

ˡr̲eference : Char -> Char -> (Char, Box Char)

function · line 197

An array reference "A[i,j]" as (name, indices): a word, then its indices in brackets that end the text, cut at the top-level commas; else a refusal.

ˡr̲eference ← { where t →
  open ← "[" ˡa̲t t
  name ← ᵗt̲rim (open − 1) t̲ake t
  _ ← ((0 < open) ∧ ˡw̲ord? name) ˡr̲equire (where, "an array reference is a name, then its indices in brackets: " c̲at t)
  _ ← (("[", "]") ˡc̲losed (open − 1) d̲rop t) ˡr̲equire (where, "an array reference ends with its closing bracket; nothing follows it: " c̲at t)
  (name, "," ˡs̲plit ˡi̲nner (open − 1) d̲rop t)
}

Characters

lowers : Char

value (private) · line 207
lowers ← "abcdefghijklmnopqrstuvwxyz"

uppers : Char

value (private) · line 208
uppers ← "ABCDEFGHIJKLMNOPQRSTUVWXYZ"

digits : Char

value (private) · line 209
digits ← "0123456789"

ʰp̲ickText : Truthy a => a -> (Char, Char) -> Char

function (private) · line 212

a when c holds, else b (texts, already computed).

ʰp̲ickText ← { c (a, b) →
  c ? a
  b
}
Used in: ˡp̲arts