This repository has been archived by the owner on Apr 29, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathproj.hs
111 lines (101 loc) · 2.69 KB
/
proj.hs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
{-# LANGUAGE GeneralizedNewtypeDeriving #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE StandaloneDeriving #-}
{-# LANGUAGE TypeFamilies #-}
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE OverloadedLists #-}
{-# LANGUAGE LambdaCase #-}
{-# OPTIONS_GHC -Wall #-}
import Data.List.NonEmpty (NonEmpty(..))
import Data.String
newtype Title =
Title String
deriving (IsString, Show)
data CompletionKey
= VariableName
| OperatorName
deriving (Show)
newtype Delim =
Delim String
deriving (Show, IsString)
data Tree
= Keyword String
| IntLit (Maybe Int)
| ArbitraryText (Maybe String)
| Variable CompletionKey (Maybe String)
| Choice Title (NonEmpty Tree) (Maybe Tree)
| List
Title
Tree
Delim
(Maybe (NonEmpty Tree))
| Composite
Title
(NonEmpty Tree)
(Maybe (NonEmpty Tree))
deriving (Show)
data Cursor
= InList Int Cursor
| InComposite Int Cursor
| InChoice Cursor
| Here
deriving (Show)
data Preview
= KeywordPreview String
| CompletionType String
| Choices Title
| ListOf Title
| CompositePreview Title
| IntLitPreview
| ArbitraryTextPreview
deriving (Show)
grammar :: Tree
grammar = expr
where
expr = Choice "expression" [let', app, op, list, tuple, parens, lit] Nothing
parens = Composite "(..)" [Keyword "(", expr, Keyword ")"] Nothing
let' =
Composite
"let"
[Keyword "let", List "definitions" def ";" Nothing]
Nothing
def =
Composite
"definition"
[Variable VariableName Nothing, Keyword "=", expr, Keyword "in", expr]
Nothing
app = Composite "application" [expr, expr] Nothing
op = Composite "infix" [expr, Variable OperatorName Nothing, expr] Nothing
list =
Composite
"list"
[Keyword "[", List "list" expr "," Nothing, Keyword "]"]
Nothing
tuple =
Composite
"tuple"
[Keyword "(", List "tuple" expr "," Nothing, Keyword ")"]
Nothing
lit = Choice "literal" [int, string] Nothing
int = IntLit Nothing
string =
Composite
"string"
[Keyword "\"", ArbitraryText Nothing, Keyword "\""]
Nothing
previewTree :: Tree -> Preview
previewTree =
\case
Keyword kw -> KeywordPreview kw
Variable completionKey _ -> previewCompletionKey completionKey
Choice title _ _ -> Choices title
List title _ _ _ -> ListOf title
Composite title _ _ -> CompositePreview title
IntLit _ -> IntLitPreview
ArbitraryText _ -> ArbitraryTextPreview
previewCompletionKey :: CompletionKey -> Preview
previewCompletionKey =
\case
VariableName -> CompletionType "variable-name"
OperatorName -> CompletionType "operator"