Skip to content

Commit e777135

Browse files
committed
Adding new lexer / parser tests
1 parent f9c29a6 commit e777135

File tree

3 files changed

+112
-7
lines changed

3 files changed

+112
-7
lines changed

syntax/src/main/jflex/test2.flex

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,12 @@ public AST ast;
9999
popState();
100100
}
101101

102+
private void blockBegin() {
103+
System.out.println("block begin!");
104+
pushAST();
105+
pushState(BLOCK);
106+
}
107+
102108

103109

104110

@@ -146,21 +152,25 @@ IDENT = {VAR} | {CONS}
146152
// States //
147153
////////////
148154

155+
%state BLOCK
149156
%state GROUPED
150157

151158

152159
%%
153160

154-
{IDENT} {appendExprSegment(ast());}
155-
{GROUP_BEGIN} {groupBegin();}
156-
161+
{IDENT} {appendExprSegment(ast());}
162+
{GROUP_BEGIN} {groupBegin();}
163+
{EOL}+{BLOCK_BEGIN} {blockBegin();}
157164

158165
<GROUPED> {
159166
{GROUP_END} {groupEnd(true);}
160167
[^] {rewind(); groupEnd(false);}
161168
}
162169

170+
<BLOCK> {
171+
{EOL}+ {System.out.println("push line!");}
172+
}
163173

164174
{EOF} {}
165175

166-
// [^] {System.out.println("UNRECOGNIZED"); System.out.println(yytext());}
176+
[^] {System.out.println("UNRECOGNIZED"); System.out.println(yytext());}

syntax/src/main/scala/org/enso/syntax/Main.scala

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ object Main extends App {
1717

1818
def pprint(s: String) {
1919
print(" " * indent)
20-
val (l, r2) = s.span((x) => (x != '(' && x != ')'))
20+
val (l, r2) = s.span(x => x != '(' && x != ')')
2121
print(l)
2222
if (r2 == "") {
2323
println
@@ -39,8 +39,7 @@ object Main extends App {
3939
}
4040

4141
// val str = "a (b"
42-
val str =
43-
"""|t (a b)""".stripMargin
42+
val str = "a\n b\n c" // .stripMargin
4443
println(str)
4544
val reader = new StringReader(str)
4645
val ss = new Lexer(reader)
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
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

Comments
 (0)