-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
284 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
c++-parser.js | ||
c++.js | ||
examples |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,118 @@ | ||
|
||
var ast = ast || {}; | ||
|
||
/* Model of inheritance */ | ||
|
||
ast.__inherit__ = (function() { | ||
var F = function (typename) { | ||
this.typename = typename; | ||
}; | ||
return function (typename, C, P) { | ||
F.prototype = P.prototype; | ||
C.prototype = new F(typename); | ||
C.uber = P.prototype; | ||
C.prototype.constructor = C; | ||
} | ||
})(); | ||
|
||
/* Node type */ | ||
|
||
ast.Node = function () {}; | ||
|
||
ast.Node.prototype = { | ||
typename: "Node", | ||
accept: function (visitor) { | ||
var methodName = 'v' + this.typename; | ||
if (methodName in visitor) { | ||
visitor[methodName].call(visitor, this); | ||
} else if ('vNode' in visitor) { | ||
visitor['vNode'].call(visitor, this); | ||
} | ||
}, | ||
}; | ||
|
||
ast.NodeList = function() { | ||
this.children = []; | ||
} | ||
|
||
ast.__inherit__("NodeList", ast.NodeList, ast.Node); | ||
|
||
ast.NodeList.prototype.add = function (obj) { | ||
this.children.push(obj); | ||
} | ||
|
||
|
||
/* Construct node types */ | ||
|
||
function nodeTypeMaker(Base) { | ||
return function (typename) { | ||
var NewType = function (obj) { | ||
for (var prop in obj) { | ||
if (obj.hasOwnProperty(prop)) { | ||
this[prop] = obj[prop]; | ||
} | ||
} | ||
} | ||
ast.__inherit__(typename, NewType, Base); | ||
ast[typename] = NewType; | ||
} | ||
} | ||
|
||
ast.makeNodeType = nodeTypeMaker(ast.Node); | ||
|
||
/* Create Node Types */ | ||
|
||
|
||
var types = [ | ||
"IncludeDirective", | ||
"UsingDirective", | ||
"ForStmt", | ||
"WhileStmt", | ||
]; | ||
for (var i in types) { | ||
ast.makeNodeType(types[i]); | ||
} | ||
|
||
/* Visitors */ | ||
|
||
ast.reportVisitor = { | ||
vNode: function(obj) { | ||
console.log("seen a Node!"); | ||
}, | ||
vIncludeDirective: function (obj) { | ||
console.log("seen an Include!"); | ||
}, | ||
vNodeList: function (obj) { | ||
console.log("seen a NodeList!"); | ||
for (var i in obj.children) { | ||
obj.children[i].accept(this); | ||
} | ||
} | ||
} | ||
|
||
ast.nodeCount = function () { | ||
this.count = 0; | ||
} | ||
|
||
ast.nodeCount.prototype = { | ||
vNode: function(obj) { | ||
this.count++; | ||
} | ||
} | ||
|
||
/* test... */ | ||
|
||
var util = require('util'); | ||
|
||
var i = new ast.IncludeDirective({ name: "iostream" }); | ||
var n = new ast.Node(); | ||
var L = new ast.NodeList(); | ||
L.add(i); | ||
L.add(n); | ||
console.log(util.inspect(L, true, 4)) | ||
|
||
L.accept(ast.reportVisitor); | ||
|
||
var nc = new ast.nodeCount(); | ||
i.accept(nc); | ||
console.log(nc.count); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,123 @@ | ||
|
||
start = | ||
__ program:Program __ { return program; } | ||
|
||
WhiteSpace = [ \t\f] | ||
LineTerminator = [\n\r] | ||
LineTerminatorSeq = "\n" / "\r\n" / "\r" | ||
SourceCharacter = . | ||
|
||
__ = | ||
(WhiteSpace / LineTerminatorSeq / Comment)* | ||
|
||
_ = | ||
(WhiteSpace / MultiLineCommentOneLine / SingleLineComment)* | ||
|
||
Literal = [0-9] | ||
|
||
MultiLineComment = | ||
"/*" (!"*/" SourceCharacter)* "*/" | ||
|
||
MultiLineCommentOneLine = | ||
"/*" (!("*/" / LineTerminator) SourceCharacter)* "*/" | ||
|
||
SingleLineComment = | ||
"//" (!LineTerminator SourceCharacter)* | ||
|
||
Comment = | ||
MultiLineComment / | ||
SingleLineComment | ||
|
||
Identifier = | ||
name:([_A-Za-z] [_A-Za-z0-9]*) { | ||
return new ast.Identifier(name); | ||
} | ||
|
||
Type = | ||
"int" / | ||
"char" / | ||
"float" | ||
|
||
FormalParameter = | ||
type:Type _ name:Identifier { | ||
return { | ||
type: type, | ||
name: name | ||
} | ||
} | ||
|
||
FormalParameterList = | ||
head:FormalParameter tail:(__ "," __ FormalParameter)* { | ||
var result = [head]; | ||
for (var i = 0; i < tail.length; i++) { | ||
result.push(tail[i][3]); | ||
} | ||
return result; | ||
} | ||
|
||
VarDecl = | ||
name:Identifier (__ "=" __ value:Literal)? { | ||
return { | ||
name: name, | ||
value: value, | ||
} | ||
} | ||
|
||
VarDeclList = | ||
head:VarDecl tail:(__ "," __ VarDecl) { | ||
var result = [head]; | ||
for (var i = 0; i < tail.length; i++) { | ||
result.push(tail[i][3]); | ||
} | ||
return result; | ||
} | ||
|
||
VarDeclStmt = | ||
type:Type __ list:VarDeclList { | ||
return new ast.VarDeclStmt(type, list); | ||
} | ||
|
||
Stmt = | ||
VarDeclStmt | ||
|
||
StmtList = | ||
head:Stmt tail:(__ Stmt)* { | ||
var result = [head]; | ||
for (var i = 0; i < tail.length; i++) { | ||
result.push(tail[i][1]); | ||
} | ||
return result; | ||
} | ||
|
||
|
||
FunctionBody = | ||
StmtList | ||
|
||
FunctionDef = | ||
Type _ name:Identifier | ||
"(" __ params:FormalParameterList? __ ")" __ | ||
"{" __ elements:FunctionBody? __ "}" { | ||
return new ast.Function(name, params, elements); | ||
} | ||
|
||
IncludeDirective "include" = | ||
"#include" _ [<"] file:[a-z]* [>"] { | ||
console.log("include!"); | ||
return new ast.Include(file) | ||
} | ||
|
||
ProgramPart = | ||
IncludeDirective / | ||
FunctionDef / | ||
Comment | ||
|
||
Program = | ||
head:ProgramPart tail:(__ ProgramPart)* { | ||
var result = [head]; | ||
for (var i = 0; i < tail.length; i++) { | ||
result.push(tail[i][1]); | ||
} | ||
return new ast.Program(result); | ||
} | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
#!/usr/bin/env node | ||
|
||
var util = require('util'); | ||
var fs = require('fs'); | ||
var cc = require('c++-parser'); | ||
|
||
fs.readFile('test.cc', 'utf-8', function (err, data) { | ||
var tree = cc.parse(data); | ||
console.log(util.inspect(tree, true, null)); | ||
}); | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
#!/bin/bash | ||
pegjs c++.pegjs c++.js | ||
cat preamble.js c++.js > c++-parser.js |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
|
||
var ast = require('ast'); | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
#include <iostream> | ||
|
||
int main() { | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters