Artifact Content
Not logged in

Artifact 6a76d51cc68a610b014d1ec92d583c65ff948177


module polemy.parse;
import polemy._common;
/*
 * Author:  k.inaba
 * License: NYSL 0.9982 (http://www.kmonos.net/nysl/
 *   Parser for the polemy programming language
 */

import polemy.lex;
import polemy.ast;
import std.bigint;

/// Parsing Failure

class ParserException : Exception
{
private:
	this(string msg) { super(msg); }
	static ParserException create(Lexer)(Lexer lex, string msg)
	{
		return new ParserException(sprintf!"[%s] %s"(
			lex.empty ? "EOF" : to!string(lex.front.pos), msg));
	}
}

/// Named Constructor of Parser

auto parserFromLexer(Lexer)(Lexer lex)
{
	return new Parser!Lexer(lex);
}

/// Named Constructor of Parser (just fwd to lexerFromString)

auto parserFromString(T...)(T params)
{
	return parserFromLexer(polemy.lex.lexerFromString(params));
}

/// Named Constructor of Parser (just fwd to lexerFromFile)

auto parserFromFile(T...)(T params)
{
	return parserFromLexer(polemy.lex.lexerFromFile(params));
}

/// Parser

class Parser(Lexer)
{
	this(Lexer lex)
	{
		this.lex = lex;
	}

	Program parseProgram()
	{
		Program p;
		while( !lex.empty )
			p ~= parseStatement();
		return p;
	}
	
	Statement parseStatement()
	{
		auto saved = lex.save;
		scope(failure) lex = saved;

		if( lex.empty )
			throw new ParserException("EOF during parsing a statement");
		auto pos = lex.front.pos;

		if( lex.front.kind==Token.Kind.identifier && lex.front.str=="var" )
		{
			// "var" Var "=" Expression ";"
			lex.popFront;
			string var = lex.front.str;
			lex.popFront;
			eat("=", "for variable declaration");
			auto parsed = new DeclStatement(pos, var, parseExpression());
			eat(";", "after variable declaration");
			return parsed;
		}
		else
		{
			// Expression ";"
			auto parsed = new ExprStatement(pos, parseExpression());
			eat(";", "after statement");
			return parsed;
		}
	}

	Expression parseExpression()
	{
		auto saved = lex.save;
		scope(failure) lex = saved;

		// Expr ::= E0
		// E0 ::= (E1 "=")* E1
		// E1 ::= (E2 "+"|"-")* E2
		// E2 ::= (E3 "*"|"/")* E3
		// E3 ::= int | str | id | "(" Expr ")"

		return parseE0();
	}

	Expression parseE0()
	{
		auto lhs = parseE1();
		if( tryEat("=") )
			lhs = new BinOpExpression(lhs.pos, "=", lhs, parseE0());
		return lhs;
	}

	Expression parseE1()
	{
		for(auto lhs = parseE2();;)
		{
			if( tryEat("+") )
				lhs = new BinOpExpression(lhs.pos, "+", lhs, parseE2());
			else if( tryEat("-") )
				lhs = new BinOpExpression(lhs.pos, "-", lhs, parseE2());
			else
				return lhs;
		}
	}

	Expression parseE2()
	{
		for(auto lhs = parseE3();;)
		{
			if( tryEat("*") )
				lhs = new BinOpExpression(lhs.pos, "*", lhs, parseE3());
			else if( tryEat("/") )
				lhs = new BinOpExpression(lhs.pos, "/", lhs, parseE3());
			else
				return lhs;
		}
	}

	Expression parseE3()
	{
		if( lex.empty )
			throw new ParserException("EOF during parsing an expression");
		auto pos = lex.front.pos;

		if( lex.front.kind == Token.Kind.number )
		{
			scope(exit) lex.popFront;
			return new IntLiteralExpression(pos, BigInt(cast(string)lex.front.str));
		}
		if( lex.front.kind == Token.Kind.stringLiteral )
		{
			scope(exit) lex.popFront;
			return new StrLiteralExpression(pos, lex.front.str);
		}
		if( tryEat("(") )
		{
			auto e = parseE0();
			eat(")", "after parenthesized expression");
			return e;
		}
		scope(exit) lex.popFront;
		return new VarExpression(pos, lex.front.str);
	}

private:
	Lexer lex;

	void eat(string kwd, lazy string msg)
	{
		if( !tryEat(kwd) )
		{
			auto e = ParserException.create(lex, "'"~kwd~"' is expected "~msg~" but '"
				~(lex.empty ? "EOF" : lex.front.str)~"' occured");
			throw e;
		}
	}

	bool tryEat(string kwd)
	{
		if( lex.empty || lex.front.kind!=Token.Kind.identifier || lex.front.str!=kwd )
			return false;
		lex.popFront;
		return true;
	}
}

unittest
{
	auto p = parserFromString(`
		var x = 100;
		var y = 200;
	`);
	Program prog = p.parseProgram();
	assert( prog.length == 2 );
	auto p0 = cast(DeclStatement)prog[0];
	auto p1 = cast(DeclStatement)prog[1];
	assert( p0.var == "x" );
	assert( p1.var == "y" );
	assert( (cast(IntLiteralExpression)p0.expr).data == 100 );
	assert( (cast(IntLiteralExpression)p1.expr).data == 200 );
}

unittest
{
	auto p = parserFromString(`
		var zzz = 100; # comment
		zzz = zzz + zzz * "fo\no"; # comment
			42;
	`);
	
	auto s0 = new DeclStatement(null, "zzz", new IntLiteralExpression(null, BigInt(100)));
	auto s1 = new ExprStatement(null, new BinOpExpression(null, "=",
		new VarExpression(null, "zzz"),
		new BinOpExpression(null, "+",
			new VarExpression(null, "zzz"),
			new BinOpExpression(null, "*",
				new VarExpression(null, "zzz"),
				new StrLiteralExpression(null, "fo\\no")
				))));
	auto s2 = new ExprStatement(null, new IntLiteralExpression(null, BigInt(42)));
	
	Program prog = p.parseProgram();
	assert( prog.length == 3 );
	assert( prog[0] == s0 );
	assert( prog[1] == s1 );
	assert( prog[2] == s2 );
}