lean4-htt/tmp/new-frontend/parser/stringliteral.lean
Leonardo de Moura 55626ba60d chore(library/init/lean): disable new frontend for now
We are going to start making drastic changes in the parser,
elaborator, attributes, etc. Examples:
- No View objects. I am going to implement match_syntax.
- No RecT in the parser. I am going to implement parser extensions
using an approach similar to the one I used to implement environment
extensions.
- No Parsec. I will use an approach similar to the one used in the
experiment https://github.com/leanprover/lean4/tree/master/tests/playground/parser

It is easier to perform these changes with the new frontend disabled.
I will slowly re-active it as I apply the changes.

cc @kha
2019-06-05 15:26:43 -07:00

54 lines
1.6 KiB
Text

/-
Copyright (c) 2018 Microsoft Corporation. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Leonardo de Moura
-/
prelude
import init.lean.parser.parsec
namespace Lean
namespace Parser
open MonadParsec
variables {m : Type → Type} {μ : Type} [Monad m] [MonadParsec μ m] [Alternative m]
def parseHexDigit : m Nat :=
( (do d ← digit, pure $ d.toNat - '0'.toNat)
<|> (do c ← satisfy (λ c, 'a'.val ≤ c.val && c.val ≤ 'f'.val), pure $ 10 + (c.toNat - 'a'.toNat))
<|> (do c ← satisfy (λ c, 'A'.val ≤ c.val && c.val ≤ 'F'.val), pure $ 10 + (c.toNat - 'A'.toNat)))
<?> "hexadecimal"
def parseQuotedChar : m Char :=
do it ← leftOver,
c ← any,
if c = '\\' then pure '\\'
else if c = '\"' then pure '\"'
else if c = '\'' then pure '\''
else if c = 'n' then pure '\n'
else if c = 't' then pure '\t'
else if c = 'x' then do {
d₁ ← parseHexDigit,
d₂ ← parseHexDigit,
pure $ Char.ofNat (16*d₁ + d₂) }
else if c = 'u' then do {
d₁ ← parseHexDigit,
d₂ ← parseHexDigit,
d₃ ← parseHexDigit,
d₄ ← parseHexDigit,
pure $ Char.ofNat (16*(16*(16*d₁ + d₂) + d₃) + d₄) }
else unexpectedAt "quoted character" it
def parseStringLiteralAux : Nat → String → m String
| 0 s := ch '\"' *> pure s
| (n+1) s := do
c ← any,
if c = '\\' then do c ← parseQuotedChar, parseStringLiteralAux n (s.push c)
else if c = '\"' then pure s
else parseStringLiteralAux n (s.push c)
def parseStringLiteral : m String :=
do ch '\"',
r ← remaining,
parseStringLiteralAux r ""
end Parser
end Lean