Skip to content

Commit

Permalink
Parsea 'hello, world!'
Browse files Browse the repository at this point in the history
  • Loading branch information
pauek committed Oct 9, 2011
1 parent d14dd15 commit a94f99b
Showing 1 changed file with 82 additions and 10 deletions.
92 changes: 82 additions & 10 deletions compiler/c++.pegjs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ _ =

Literal = [0-9]

DecimalDigit
= [0-9]

MultiLineComment =
"/*" (!"*/" SourceCharacter)* "*/"

Expand All @@ -33,11 +36,66 @@ Identifier =
return new ast.Identifier({ id: name });
}

Expression =
StringLiteral /
VariableReference

StringLiteral "string" =
'"' literal:StringCharacters? '"' {
return new ast.StringLiteral({ lit: literal });
}

StringCharacters =
chars:StringCharacter+ {
return chars.join("");
}

StringCharacter =
!('"' / "\\" / LineTerminator) char_:SourceCharacter { return char_; } /
"\\" sequence:EscapeSequence { return sequence; } /
LineContinuation

EscapeSequence =
CharacterEscapeSequence /
"0" !DecimalDigit { return "\0"; }

CharacterEscapeSequence =
SingleEscapeCharacter /
NonEscapeCharacter

SingleEscapeCharacter =
char_:['"\\bfnrtv] {
return char_
.replace("b", "\b")
.replace("f", "\f")
.replace("n", "\n")
.replace("r", "\r")
.replace("t", "\t")
.replace("v", "\x0B") // IE does not recognize "\v".
}

NonEscapeCharacter =
(!EscapeCharacter / LineTerminator) char_:SourceCharacter {
return char_;
}

EscapeCharacter =
SingleEscapeCharacter /
DecimalDigit

LineContinuation
= "\\" sequence:LineTerminatorSequence { return sequence; }

LineTerminatorSequence "end of line" = "\n" / "\r\n" / "\r"

Type =
"int" /
"char" /
"float"

VariableReference =
id:Identifier { return new ast.VariableReference({ var: id }); }

FormalParameter =
type:Type _ name:Identifier {
return {
Expand Down Expand Up @@ -74,12 +132,22 @@ VariableDeclarationList =
}

VariableDeclarationStatement =
t:Type __ v:VariableDeclarationList __ ";" {
return new ast.VariableDeclarationStatement({ type: t, list: v });
t:Type __ lst:VariableDeclarationList __ ";" {
return new ast.VariableDeclarationStatement({ type: t }, lst);
}

OutputStatement =
"cout" elems:(__ "<<" __ Expression)* ";" {
var elements = [];
for (var i in elems) {
elements.push(elems[i][3]);
}
return new ast.OutputStatement({ head: "cout" }, elements);
}

Statement =
VariableDeclarationStatement
VariableDeclarationStatement /
OutputStatement

StatementList =
head:Statement tail:(__ Statement)* {
Expand All @@ -97,27 +165,31 @@ FunctionBody =
FunctionDef =
Type _ n:Identifier
"(" __ p:FormalParameterList? __ ")" __
"{" __ e:FunctionBody? __ "}" {
return new ast.FunctionDef({ name: n, params: p, body: e });
"{" __ body:FunctionBody? __ "}" {
return new ast.FunctionDef({ name: n, params: p }, body);
}

IncludeDirective "include" =
"#include" _ [<"] file:[a-z]* [>"] {
return new ast.IncludeDirective({ file: file });
}

UsingDirective "using" =
"using" __ "namespace" __ ns:Identifier __ ";" {
return new ast.UsingDirective({ namespace: ns });
}

ProgramPart =
IncludeDirective /
UsingDirective /
FunctionDef /
Comment

Program =
head:ProgramPart tail:(__ ProgramPart)* {
var result = [head];
var parts = [head];
for (var i = 0; i < tail.length; i++) {
result.push(tail[i][1]);
parts.push(tail[i][1]);
}
return new ast.Program({ parts: result });
return new ast.Program({}, parts);
}


0 comments on commit a94f99b

Please sign in to comment.