|
| 1 | +package org.enso.syntax.text.parser |
| 2 | + |
| 3 | +import java.io.Reader |
| 4 | + |
| 5 | +////////////////// |
| 6 | +// Parser Rules // |
| 7 | +////////////////// |
| 8 | + |
| 9 | +trait Result[Input, Output] |
| 10 | +case class Fail[Input, Output](rest: Input, |
| 11 | + contexts: List[String], |
| 12 | + error: String) |
| 13 | + extends Result[Input, Output] |
| 14 | +case class Partial[Input, Output](cont: Input => Result[Input, Output]) |
| 15 | + extends Result[Input, Output] |
| 16 | +case class Done[Input, Output](rest: Input, output: Output) |
| 17 | + extends Result[Input, Output] |
| 18 | + |
| 19 | +case class Pos(offset: Int) |
| 20 | + |
| 21 | +trait More |
| 22 | +case object Complete extends More |
| 23 | +case object Incomplete extends More |
| 24 | + |
| 25 | +class PP() { |
| 26 | + type Input = Int |
| 27 | + type Output = Int |
| 28 | + type State = Int |
| 29 | + |
| 30 | + type Failure = |
| 31 | + (State, Pos, More, List[String], String) => Result[Input, Output] |
| 32 | + type Success = (State, Pos, More, Output) => Result[Input, Output] |
| 33 | + |
| 34 | + //failK :: Failure a |
| 35 | + //failK t (Pos pos) _more stack msg = Fail (Buf.dropWord16 pos t) stack msg |
| 36 | + def failK(state: State, |
| 37 | + pos: Pos, |
| 38 | + more: More, |
| 39 | + stack: List[String], |
| 40 | + msg: String): Result[Input, Output] = Fail(0 /*!*/, stack, msg) |
| 41 | + |
| 42 | + //successK :: Success a a |
| 43 | + //successK t (Pos pos) _more a = Done (Buf.dropWord16 pos t) a |
| 44 | + def successK(state: State, |
| 45 | + pos: Pos, |
| 46 | + more: More, |
| 47 | + a: Input): Result[Input, Output] = |
| 48 | + Done(0 /*!*/, a) |
| 49 | +// ensure :: Int -> Parser (Pos, Text) |
| 50 | +// ensure n = T.Parser $ \t pos more lose succ -> |
| 51 | +// case lengthAtLeast pos n t of |
| 52 | +// Just n' -> succ t pos more (n', substring pos n' t) |
| 53 | +// -- The uncommon case is kept out-of-line to reduce code size: |
| 54 | +// Nothing -> ensureSuspended n t pos more lose succ |
| 55 | + |
| 56 | +// def ensure(n:Int): Parser2 = |
| 57 | +// |
| 58 | +// class Parser2( |
| 59 | +// run: (State, Pos, More, Failure, Success) => Result[Input, Output]) { |
| 60 | +// |
| 61 | +// //parse :: Parser a -> Text -> Result a |
| 62 | +// //parse m s = runParser m (buffer s) 0 Incomplete failK successK |
| 63 | +// def parse(input: Input): Result[Input, Output] = |
| 64 | +// this.run(input, Pos(0), Incomplete, failK, successK) |
| 65 | +// } |
| 66 | +} |
| 67 | +// |
| 68 | +//-- | Run a parser. |
| 69 | +//parse :: Parser a -> Text -> Result a |
| 70 | +//parse m s = runParser m (buffer s) 0 Incomplete failK successK |
| 71 | +//newtype Parser i a = Parser { |
| 72 | +//runParser :: forall r. |
| 73 | +//State i -> Pos -> More |
| 74 | +//-> Failure i (State i) r |
| 75 | +//-> Success i (State i) a r |
| 76 | +//-> IResult i r |
| 77 | +//} |
| 78 | +// |
| 79 | +//type family State i |
| 80 | +//type instance State ByteString = B.Buffer |
| 81 | +//type instance State Text = T.Buffer |
| 82 | +// |
| 83 | +//type Failure i t r = t -> Pos -> More -> [String] -> String |
| 84 | +//-> IResult i r |
| 85 | +//type Success i t a r = t -> Pos -> More -> a -> IResult i r |
| 86 | +// |
| 87 | +//-- | Have we read all available input? |
| 88 | +//data More = Complete | Incomplete |
| 89 | +//deriving (Eq, Show) |
| 90 | +//newtype Parser i a = Parser { |
| 91 | +// runParser :: forall r. |
| 92 | +// State i -> Pos -> More |
| 93 | +// -> Failure i (State i) r |
| 94 | +// -> Success i (State i) a r |
| 95 | +// -> IResult i r |
| 96 | +//} |
0 commit comments