Add string literals to the term language

Signed-off-by: Danila Fedorin <danila.fedorin@gmail.com>
This commit is contained in:
Danila Fedorin 2023-12-01 12:55:11 -08:00
parent 45a04cc59c
commit 546265f2e6
2 changed files with 38 additions and 1 deletions

View File

@ -24,7 +24,7 @@ termToLatex f t =
Call s ts -> "\\text{" ++ s ++ "}(" ++ String.join "," (List.map (termToLatex f) ts) ++ ")"
Var x -> f x
IntLit i -> String.fromInt i
StringLit s -> "\"" ++ s ++ "\""
StringLit s -> "\\text{" ++ "``" ++ s ++ "''" ++ "}"
metavariableToLatex : Metavariable -> String
metavariableToLatex (MkMetavariable s) =

View File

@ -9,6 +9,42 @@ import Set
intLit : Parser Int
intLit = Parser.int
decodeStr : String -> String
decodeStr str =
let
go l =
case l of
'\\' :: 'n' :: rest -> '\n' :: go rest
'\\' :: '\\' :: rest -> '\\' :: go rest
'\\' :: '"' :: rest -> '"' :: go rest
'\\' :: c :: rest -> c :: go rest
c :: rest -> c :: go rest
[] -> []
noQuotes = String.dropLeft 1 <| String.dropRight 1 <| str
in
String.fromList (go (String.toList str))
strLit : Parser String
strLit =
let
char = Parser.map decodeStr <| Parser.getChompedString <|
Parser.oneOf
[ Parser.backtrackable <|
Parser.chompIf (\c -> c == '\\')
|. Parser.chompIf (\c -> True)
, Parser.backtrackable <| Parser.chompIf (\c -> c /= '"')
]
in
Parser.map (String.join "") <| Parser.sequence
{ start = "\""
, separator = ""
, end = "\""
, spaces = Parser.succeed ()
, item = char
, trailing = Optional
}
name : Parser String
name = Parser.variable
{ start = \c -> Char.isAlphaNum c || c == '_'
@ -44,6 +80,7 @@ term = Parser.lazy (\() -> Parser.oneOf
, Parser.backtrackable <|
Parser.succeed Var |= variable
, Parser.succeed IntLit |= intLit
, Parser.succeed StringLit |= strLit
])
rule : Parser Rule