Theory Lexer_Command_Tests
theory Lexer_Command_Tests
imports Codepoint_Tests TIGER_Lexer.Lexer_Command
begin
section ‹Tests of the lexer command›
subsection ‹Identifiers and operators›
text ‹The rules of ‹Codepoint_Tests› as a @{command lexer}. Without ‹modes› the mode
type is \<^typ>‹unit›:›
lexer cp :: ‹tok›
where
lower = ‹[a-zα-κμ-ω]› | upper = ‹[A-ZΑ-ΡΣ-Ω]›
| idchar = ‹lower | upper | [0-9_']›
| many(p) = ‹p p*›
rules
‹lower idchar*› ⇒ LIDENT
| ‹upper idchar*› ⇒ UIDENT
| ‹many([\u{2190}-\u{22FF}])› ⇒ OP
| ‹many([ \n])› ⇒ skip
lemma ‹cp.spec () =
[⦇match = Times (Pred cp.lower) (Star (Pred cp.idchar)),
act = mk_act (λk m. m) (λu. Some LIDENT) (λu. True) Keep⦈,
⦇match = Times (Pred cp.upper) (Star (Pred cp.idchar)),
act = mk_act (λk m. m) (λu. Some UIDENT) (λu. True) Keep⦈,
⦇match = Times (Pred [(0x2190, 0x22FF)]) (Star (Pred [(0x2190, 0x22FF)])),
act = mk_act (λk m. m) (λu. Some OP) (λu. True) Keep⦈,
⦇match = Times (Pred [(0x0A, 0x0A), (0x20, 0x20)]) (Star (Pred [(0x0A, 0x0A), (0x20, 0x20)])),
act = mk_act (λk m. m) (λu. None) (λu. True) Keep⦈]›
by simp
text ‹The regexes are those of \<^const>‹cp_rules› up to the normal form of classes, and
the actions agree:›
lemma ‹map (map_rexp cp_norm ∘ match) (cp.spec ()) = map (map_rexp cp_norm ∘ match) (hd cp_rules)›
by eval
lemma ‹map (λr. fst (act r u [()])) (cp.spec ()) = [Some LIDENT, Some UIDENT, Some OP, None]›
by (simp add: mk_act_def)
lemma ‹cp.start = ()› ‹cp.final ()› ‹cp.is_newline 0x0A› ‹¬ cp.is_newline 0x20›
by (simp_all add: cp.start_def cp.final_def cp.is_newline_def)
subsection ‹Nested comments›
text ‹A mode for comments, entered by ‹push› and left by ‹pop›. ‹Comment› is exclusive,
so the untagged rules are not active there:›
datatype ctok = Word | Num