| 1 |
-- Taken from README.Data.Trie.NonDependent |
| 2 |
|
| 3 |
{-# OPTIONS --guardedness --sized-types #-} |
| 4 |
|
| 5 |
module Main where |
| 6 |
|
| 7 |
open import Level |
| 8 |
open import Data.Unit |
| 9 |
open import Data.Bool |
| 10 |
open import Data.Char as Char hiding (show) |
| 11 |
import Data.Char.Properties as Char |
| 12 |
open import Data.List.Base as List using (List; []; _∷_) |
| 13 |
open import Data.List.Fresh as List# using (List#; []; _∷#_) |
| 14 |
open import Data.Maybe as Maybe |
| 15 |
open import Data.Product as Prod |
| 16 |
open import Data.String as String using (String; unlines; _++_) |
| 17 |
open import Data.These as These |
| 18 |
|
| 19 |
open import Function.Base using (case_of_; _$_; _∘′_; id; _on_) |
| 20 |
open import Relation.Nary |
| 21 |
open import Relation.Binary.Core using (Rel) |
| 22 |
open import Relation.Nullary.Decidable using (¬?) |
| 23 |
|
| 24 |
open import Data.Trie Char.<-strictTotalOrder |
| 25 |
open import Data.Tree.AVL.Value |
| 26 |
|
| 27 |
open import IO.Base |
| 28 |
open import IO.Finite |
| 29 |
|
| 30 |
record Lexer t : Set (suc t) where |
| 31 |
field |
| 32 |
Tok : Set t |
| 33 |
|
| 34 |
Keyword : Set t |
| 35 |
Keyword = String × Tok |
| 36 |
|
| 37 |
Distinct : Rel Keyword 0ℓ |
| 38 |
Distinct a b = ⌊ ¬? ((proj₁ a) String.≟ (proj₁ b)) ⌋ |
| 39 |
|
| 40 |
field |
| 41 |
keywords : List# Keyword Distinct |
| 42 |
breaking : Char → ∃ λ b → if b then Maybe Tok else Lift _ ⊤ |
| 43 |
default : String → Tok |
| 44 |
|
| 45 |
|
| 46 |
module _ {t} (L : Lexer t) where |
| 47 |
open Lexer L |
| 48 |
|
| 49 |
tokenize : String → List Tok |
| 50 |
tokenize = start ∘′ String.toList where |
| 51 |
|
| 52 |
mutual |
| 53 |
|
| 54 |
Keywords : Set _ |
| 55 |
Keywords = Trie (const _ Tok) _ |
| 56 |
|
| 57 |
init : Keywords |
| 58 |
init = fromList $ List.map (Prod.map₁ String.toList) $ proj₁ $ List#.toList keywords |
| 59 |
|
| 60 |
start : List Char → List Tok |
| 61 |
start = loop [] init |
| 62 |
|
| 63 |
loop : (acc : List Char) → -- chars read so far in this token |
| 64 |
(toks : Keywords) → -- keyword candidates left at this point |
| 65 |
(input : List Char) → -- list of chars to tokenize |
| 66 |
List Tok |
| 67 |
loop acc toks [] = push acc [] |
| 68 |
loop acc toks (c ∷ cs) = case breaking c of λ where |
| 69 |
(true , m) → push acc $ maybe′ _∷_ id m $ start cs |
| 70 |
(false , _) → case lookupValue toks (c ∷ []) of λ where |
| 71 |
(just tok) → tok ∷ start cs |
| 72 |
nothing → loop (c ∷ acc) (lookupTrie toks c) cs |
| 73 |
|
| 74 |
push : List Char → List Tok → List Tok |
| 75 |
push [] ts = ts |
| 76 |
push cs ts = default (String.fromList (List.reverse cs)) ∷ ts |
| 77 |
|
| 78 |
module LetIn where |
| 79 |
|
| 80 |
data TOK : Set where |
| 81 |
LET EQ IN : TOK |
| 82 |
LPAR RPAR : TOK |
| 83 |
ID : String → TOK |
| 84 |
|
| 85 |
show : TOK → String |
| 86 |
show LET = "LET" |
| 87 |
show EQ = "EQ" |
| 88 |
show IN = "IN" |
| 89 |
show LPAR = "LPAR" |
| 90 |
show RPAR = "RPAR" |
| 91 |
show (ID x) = "ID \"" ++ x ++ "\"" |
| 92 |
|
| 93 |
keywords : List# (String × TOK) (λ a b → ⌊ ¬? ((proj₁ a) String.≟ (proj₁ b)) ⌋) |
| 94 |
keywords = ("let" , LET) |
| 95 |
∷# ("=" , EQ) |
| 96 |
∷# ("in" , IN) |
| 97 |
∷# [] |
| 98 |
|
| 99 |
breaking : Char → ∃ (λ b → if b then Maybe TOK else Lift 0ℓ ⊤) |
| 100 |
breaking c = if isSpace c then true , nothing else parens c where |
| 101 |
|
| 102 |
parens : Char → _ |
| 103 |
parens '(' = true , just LPAR |
| 104 |
parens ')' = true , just RPAR |
| 105 |
parens _ = false , _ |
| 106 |
|
| 107 |
default : String → TOK |
| 108 |
default = ID |
| 109 |
|
| 110 |
letIn : Lexer 0ℓ |
| 111 |
letIn = record { LetIn } |
| 112 |
|
| 113 |
main : Main |
| 114 |
main = run $ do |
| 115 |
let open LetIn |
| 116 |
putStrLn $ unlines $ List.map show $ |
| 117 |
tokenize letIn "fix f x = let b = fix f in (f b) x" |