Differences From Artifact [25ec86a59533b939]:
- File
polemy/parse.d
- 2010-11-21 08:33:47 - part of checkin [6ecc7046fc] on branch trunk - tableset x{y:1} expression added (user: kinaba) [annotate]
To 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]
5 * Parser for Polemy programming language 5 * Parser for Polemy programming language
6 */ 6 */
7 module polemy.parse; 7 module polemy.parse;
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 13
13 /// Parse a string and return its AST 14 /// Parse a string and return its AST
14 /// Throws: ParseException, LexException, UnexpectedEOF 15 /// Throws: ParseException, LexException, UnexpectedEOF
15 16
16 AST parseString(S, T...)(S str, T fn_ln_cn) 17 AST parseString(S, T...)(S str, T fn_ln_cn)
17 { 18 {
18 return parserFromString(str, fn_ln_cn).parse(); 19 return parserFromString(str, fn_ln_cn).parse();
................................................................................................................................................................................
98 string kwd = "@" ~ layer; 99 string kwd = "@" ~ layer;
99 string var = layer; 100 string var = layer;
100 101
101 auto e = tryEat("(") 102 auto e = tryEat("(")
102 ? parseLambdaAfterOpenParen(pos) // let var ( . 103 ? parseLambdaAfterOpenParen(pos) // let var ( .
103 : (eat("=", "after "~kwd), E(0)); // let var = . 104 : (eat("=", "after "~kwd), E(0)); // let var = .
104 if( moreDeclarationExists() ) 105 if( moreDeclarationExists() )
105 return new LetExpression(pos, var, "(system)", e | 106 return new LetExpression(pos, var, SystemLayer,
106 else 107 else
107 return new LetExpression(pos, var, "(system)", e | 108 return new LetExpression(pos, var, SystemLayer,
108 } 109 }
109 else 110 else
110 { 111 {
111 string kwd = layer; 112 string kwd = layer;
112 if( layer.empty && !tryEat(kwd="let") && !tryEat(kwd="va 113 if( layer.empty && !tryEat(kwd="let") && !tryEat(kwd="va
113 return null; // none of {@lay, let, var, def} oc 114 return null; // none of {@lay, let, var, def} oc
114 115
................................................................................................................................................................................
471 assert_eq(parseString(`def foo(x) { x+1 }; foo`), 472 assert_eq(parseString(`def foo(x) { x+1 }; foo`),
472 let("foo", "", 473 let("foo", "",
473 fun(["x"], call(var("+"), var("x"), intl(1))), 474 fun(["x"], call(var("+"), var("x"), intl(1))),
474 var("foo")) 475 var("foo"))
475 ); 476 );
476 477
477 assert_eq(parseString(`@@type ( x ) { x }`), 478 assert_eq(parseString(`@@type ( x ) { x }`),
478 let("@type", "(system)", fun(["x"], var("x")), var("@type")) ); | 479 let("@type", SystemLayer, fun(["x"], var("x")), var("@type")) );
479 480
480 assert_eq(parseString(`{}`), call(var("{}"))); 481 assert_eq(parseString(`{}`), call(var("{}")));
481 assert_eq(parseString(`{foo:1,"bar":2}`), 482 assert_eq(parseString(`{foo:1,"bar":2}`),
482 call(var(".="), call(var(".="), call(var("{}")), strl("foo"), in 483 call(var(".="), call(var(".="), call(var("{}")), strl("foo"), in
483 assert_eq(parseString(`{}.foo`), call(var("."),call(var("{}")),strl("foo 484 assert_eq(parseString(`{}.foo`), call(var("."),call(var("{}")),strl("foo
484 assert_eq(parseString(`{}.?foo`), call(var(".?"),call(var("{}")),strl("f 485 assert_eq(parseString(`{}.?foo`), call(var(".?"),call(var("{}")),strl("f
485 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(
486 } 487 }