Add a parser for the tiny language

Signed-off-by: Danila Fedorin <danila.fedorin@gmail.com>
This commit is contained in:
Danila Fedorin 2023-11-26 11:40:15 -08:00
parent 6271dd8c2b
commit 295c93e38a
2 changed files with 61 additions and 1 deletions

View File

@ -8,7 +8,8 @@
"direct": {
"elm/browser": "1.0.2",
"elm/core": "1.0.5",
"elm/html": "1.0.0"
"elm/html": "1.0.0",
"elm/parser": "1.1.0"
},
"indirect": {
"elm/json": "1.1.3",

59
src/Bergamot/Parser.elm Normal file
View File

@ -0,0 +1,59 @@
module Bergamot.Parser exposing (..)
import Bergamot.Syntax exposing (Term(..), Metavariable)
import Bergamot.Rules exposing (Rule)
import Parser exposing (Parser, Trailing(..), (|.), (|=))
import Set
intLit : Parser Int
intLit = Parser.int
name : Parser String
name = Parser.variable
{ start = \c -> Char.isAlphaNum c || c == '_'
, inner = \c -> Char.isAlphaNum c || c == '_'
, reserved = Set.empty
}
variable : Parser String
variable = Parser.variable
{ start = \c -> c == '?'
, inner = \c -> Char.isAlphaNum c || c == '_'
, reserved = Set.empty
}
term : Parser (Term Metavariable)
term = Parser.lazy (\() -> Parser.oneOf
[ Parser.succeed IntLit |= intLit
, Parser.succeed Call
|= name
|= Parser.sequence
{ start = "("
, separator = ","
, end = ")"
, spaces = Parser.spaces
, item = term
, trailing = Forbidden
}
, Parser.succeed Var |= variable
])
rule : Parser Rule
rule =
let
makeRule n c ps = { name = n, conclusion = c, premises = ps }
in
Parser.succeed makeRule
|= name
|. Parser.spaces |. Parser.symbol "@" |. Parser.spaces
|= term
|. Parser.spaces |. Parser.symbol "<-" |. Parser.spaces
|= Parser.sequence
{ start = ""
, separator = ","
, end = ""
, spaces = Parser.spaces
, item = term
, trailing = Forbidden
}