Differences From Artifact [71d187282fb3ce05]:
- File
polemy/parse.d
- 2010-11-21 09:53:17 - part of checkin [435fa085ec] on branch trunk - refactored predefined layer names, and filled readme.txt. (user: kinaba) [annotate]
To Artifact [2e1caca9f119fc4c]:
- File
polemy/parse.d
- 2010-11-21 11:11:49 - part of checkin [2bdfb8a182] on branch trunk - moved build sciprt for documents into Poseidon environment (user: kinaba) [annotate]
8 import polemy._common; 8 import polemy._common;
9 import polemy.failure; 9 import polemy.failure;
10 import polemy.lex; 10 import polemy.lex;
11 import polemy.ast; 11 import polemy.ast;
12 import polemy.layer; 12 import polemy.layer;
13 13
14 /// Parse a string and return its AST 14 /// Parse a string and return its AST
15 /// Throws: ParseException, LexException, UnexpectedEOF <
16 15
17 AST parseString(S, T...)(S str, T fn_ln_cn) 16 AST parseString(S, T...)(S str, T fn_ln_cn)
18 { 17 {
19 return parserFromString(str, fn_ln_cn).parse(); 18 return parserFromString(str, fn_ln_cn).parse();
20 } 19 }
21 20
22 /// Parse the content of a file and return its AST 21 /// Parse the content of a file and return its AST
23 /// Throws: ParseException, LexException, UnexpectedEOF <
24 22
25 AST parseFile(S, T...)(S filename, T ln_cn) 23 AST parseFile(S, T...)(S filename, T ln_cn)
26 { 24 {
27 return parserFromFile(filename, ln_cn).parse(); 25 return parserFromFile(filename, ln_cn).parse();
28 } 26 }
29 27
30 // Named Constructors of Parser 28 // Named Constructors of Parser
................................................................................................................................................................................
71 /// Declaration ::= 69 /// Declaration ::=
72 /// ["@" Layer|"let"|"var"|"def"] Var "=" Expression ([";"|"i 70 /// ["@" Layer|"let"|"var"|"def"] Var "=" Expression ([";"|"i
73 /// | ["@" Layer|"let"|"var"|"def"] Var "(" Param%"," ")" "{" B 71 /// | ["@" Layer|"let"|"var"|"def"] Var "(" Param%"," ")" "{" B
74 /// | ["@" "@" Layer "=" Expression ([";"|"in"] Body?)? 72 /// | ["@" "@" Layer "=" Expression ([";"|"in"] Body?)?
75 /// | ["@" "@" Layer "(" Param%"," ")" "{" Body "}" ([";"|"in"] 73 /// | ["@" "@" Layer "(" Param%"," ")" "{" Body "}" ([";"|"in"]
76 74
77 auto pos = currentPosition(); 75 auto pos = currentPosition();
78 string layer = ""; | 76 Layer layer = "";
79 bool layerRiseDecl = false; | 77 bool layerLiftDecl = false;
80 78
81 if( tryEat("@") ) 79 if( tryEat("@") )
82 { 80 {
83 layer = "@" ~ eatId("after @", AllowQuoted); 81 layer = "@" ~ eatId("after @", AllowQuoted);
84 if( layer == "@@" ) 82 if( layer == "@@" )
85 { 83 {
86 layer = "@" ~ eatId("after @@", AllowQuoted); 84 layer = "@" ~ eatId("after @@", AllowQuoted);
87 layerRiseDecl = true; | 85 layerLiftDecl = true;
88 } 86 }
89 else 87 else
90 { 88 {
91 if( tryEat("(") ) 89 if( tryEat("(") )
92 return null; // @lay(...) expression, no 90 return null; // @lay(...) expression, no
93 } 91 }
94 } 92 }
95 93
96 // [TODO] Refactor 94 // [TODO] Refactor
97 if( layerRiseDecl ) | 95 if( layerLiftDecl )
98 { 96 {
99 string kwd = "@" ~ layer; 97 string kwd = "@" ~ layer;
100 string var = layer; 98 string var = layer;
101 99
102 auto e = tryEat("(") 100 auto e = tryEat("(")
103 ? parseLambdaAfterOpenParen(pos) // let var ( . 101 ? parseLambdaAfterOpenParen(pos) // let var ( .
104 : (eat("=", "after "~kwd), E(0)); // let var = . 102 : (eat("=", "after "~kwd), E(0)); // let var = .
105 if( moreDeclarationExists() ) 103 if( moreDeclarationExists() )
106 return new LetExpression(pos, var, SystemLayer, 104 return new LetExpression(pos, var, SystemLayer,
107 else 105 else
108 return new LetExpression(pos, var, SystemLayer, | 106 return new LetExpression(pos, var, SystemLayer,
> 107 new LayeredExpression(pos, SystemLayer,
> 108 );
109 } 109 }
110 else 110 else
111 { 111 {
112 string kwd = layer; 112 string kwd = layer;
113 if( layer.empty && !tryEat(kwd="let") && !tryEat(kwd="va 113 if( layer.empty && !tryEat(kwd="let") && !tryEat(kwd="va
114 return null; // none of {@lay, let, var, def} oc 114 return null; // none of {@lay, let, var, def} oc
115 115
................................................................................................................................................................................
389 throw genex!ParseException(currentPosition(), "identifie 389 throw genex!ParseException(currentPosition(), "identifie
390 scope(exit) lex.popFront; 390 scope(exit) lex.popFront;
391 return lex.front.str; 391 return lex.front.str;
392 } 392 }
393 393
394 AST doNothingExpression() 394 AST doNothingExpression()
395 { 395 {
396 return new IntLiteral(currentPosition(), BigInt(178)); | 396 return new StrLiteral(currentPosition(), "(empty function body)"
397 } 397 }
398 398
399 immutable(LexPosition) currentPosition() 399 immutable(LexPosition) currentPosition()
400 { 400 {
401 return lex.empty ? null : lex.front.pos; 401 return lex.empty ? null : lex.front.pos;
402 } 402 }
403 } 403 }
................................................................................................................................................................................
416 assert_eq(parseString(`1;2;`), let("_","",intl(1),intl(2))); 416 assert_eq(parseString(`1;2;`), let("_","",intl(1),intl(2)));
417 assert_eq(parseString(`let x=1 in 2`), let("x","",intl(1),intl(2))); 417 assert_eq(parseString(`let x=1 in 2`), let("x","",intl(1),intl(2)));
418 assert_eq(parseString(`var x=1;2;`), let("x","",intl(1),intl(2))); 418 assert_eq(parseString(`var x=1;2;`), let("x","",intl(1),intl(2)));
419 assert_eq(parseString(`def x=1`), let("x","",intl(1),var("x"))); 419 assert_eq(parseString(`def x=1`), let("x","",intl(1),var("x")));
420 assert_eq(parseString(`@val x=1;`), let("x","@val",intl(1),var("x"))); 420 assert_eq(parseString(`@val x=1;`), let("x","@val",intl(1),var("x")));
421 assert_eq(parseString(`@typ x="#int";`), let("x","@typ",strl("#int"),var 421 assert_eq(parseString(`@typ x="#int";`), let("x","@typ",strl("#int"),var
422 assert_eq(parseString(`f(1,2)`), call(var("f"),intl(1),intl(2))); 422 assert_eq(parseString(`f(1,2)`), call(var("f"),intl(1),intl(2)));
423 assert_eq(parseString(`if(1){2}`), call(var("if"),intl(1),fun([],intl(2) | 423 assert_eq(parseString(`if(1){2}`), call(var("if"),intl(1),fun([],intl(2)
424 assert_eq(parseString(`if(1){2}else{3}`), call(var("if"),intl(1),fun([], 424 assert_eq(parseString(`if(1){2}else{3}`), call(var("if"),intl(1),fun([],
425 assert_eq(parseString(`if(1){}else{3}()()`), 425 assert_eq(parseString(`if(1){}else{3}()()`),
426 call(call(call(var("if"),intl(1),fun([],intl(178)),fun([],intl(3 | 426 call(call(call(var("if"),intl(1),fun([],strl("(empty function bo
427 assert_eq(parseString(`1+2*3`), call(var("+"),intl(1),call(var("*"),intl 427 assert_eq(parseString(`1+2*3`), call(var("+"),intl(1),call(var("*"),intl
428 assert_eq(parseString(`(1+2)*3`), call(var("*"),call(var("+"),intl(1),in 428 assert_eq(parseString(`(1+2)*3`), call(var("*"),call(var("+"),intl(1),in
429 assert_eq(parseString(`1*(2+3)`), call(var("*"),intl(1),call(var("+"),in 429 assert_eq(parseString(`1*(2+3)`), call(var("*"),intl(1),call(var("+"),in
430 assert_eq(parseString(`1*2+3`), call(var("+"),call(var("*"),intl(1),intl 430 assert_eq(parseString(`1*2+3`), call(var("+"),call(var("*"),intl(1),intl
431 assert_eq(parseString(`@x(1)`), lay("@x", intl(1))); 431 assert_eq(parseString(`@x(1)`), lay("@x", intl(1)));
432 assert_eq(parseString(`fun(x @v @t, y, z @t){}`), 432 assert_eq(parseString(`fun(x @v @t, y, z @t){}`),
433 funp([param("x",["@v","@t"]), param("y",[]), param("z",["@t"])], | 433 funp([param("x",["@v","@t"]), param("y",[]), param("z",["@t"])],
434 434
435 assert_eq(parseString(` 435 assert_eq(parseString(`
436 let x = 100; #comment 436 let x = 100; #comment
437 let y = 200; #comment!!!!! 437 let y = 200; #comment!!!!!
438 x+y 438 x+y
439 `), 439 `),
440 let("x", "", intl(100), let("y", "", intl(200), call(var("+"), v 440 let("x", "", intl(100), let("y", "", intl(200), call(var("+"), v
................................................................................................................................................................................
472 assert_eq(parseString(`def foo(x) { x+1 }; foo`), 472 assert_eq(parseString(`def foo(x) { x+1 }; foo`),
473 let("foo", "", 473 let("foo", "",
474 fun(["x"], call(var("+"), var("x"), intl(1))), 474 fun(["x"], call(var("+"), var("x"), intl(1))),
475 var("foo")) 475 var("foo"))
476 ); 476 );
477 477
478 assert_eq(parseString(`@@type ( x ) { x }`), 478 assert_eq(parseString(`@@type ( x ) { x }`),
479 let("@type", SystemLayer, fun(["x"], var("x")), var("@type")) ); | 479 let("@type", SystemLayer, fun(["x"], var("x")), lay(SystemLayer,
480 480
481 assert_eq(parseString(`{}`), call(var("{}"))); 481 assert_eq(parseString(`{}`), call(var("{}")));
482 assert_eq(parseString(`{foo:1,"bar":2}`), 482 assert_eq(parseString(`{foo:1,"bar":2}`),
483 call(var(".="), call(var(".="), call(var("{}")), strl("foo"), in 483 call(var(".="), call(var(".="), call(var("{}")), strl("foo"), in
484 assert_eq(parseString(`{}.foo`), call(var("."),call(var("{}")),strl("foo 484 assert_eq(parseString(`{}.foo`), call(var("."),call(var("{}")),strl("foo
485 assert_eq(parseString(`{}.?foo`), call(var(".?"),call(var("{}")),strl("f 485 assert_eq(parseString(`{}.?foo`), call(var(".?"),call(var("{}")),strl("f
486 assert_eq(parseString(`x{y:1}`), call(var(".="),var("x"),strl("y"),intl( 486 assert_eq(parseString(`x{y:1}`), call(var(".="),var("x"),strl("y"),intl(
487 } 487 }