Skip to content

Saartje87/js-ast-parser

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

79 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

AST parser

Javascript AST parser for 'simple' expressions.

Install

npm install -g karma-cli
npm install

Run tests

karma start

Usage

var tree = Tokenize('1 + foo');

console.log(tree);

Supported expressions

  • Numbers (1, 1.23)
  • Strings ('foo')
  • BinaryExpression (+-%/*)
  • LogicalExpression (|| &&)
  • Identifier (foo)
  • Objects (foo.bar, foo['bar'])
  • Arrays (foo[0])
  • Functions (foo(), foo('bar'))
  • Nested expressions (a || b && c)
  • Literals (true, false, null)
  • ConditionalExpressions (foo ? true : false)
  • Unary Expression (!foo, --bar)
  • Assignment Expression (foo = 'bar')

Roadmap

  • Performance
  • More operators
  • Compile AST to callable
  • Throw error when string could not be parsed
var tree = Tokenize('1 + foo');
var callable = Compile();  

callable({foo: 2}); // Outputs '3'
  • Better and more usefull errors

Add support for

  • foo++
  • foo--
  • !activeTab || activeTab === 'intern'
  • !foo || bar
  • foo.bar.baz['a']['b']

Todo tests

foo['a'].bar.baz
// fails, outputs. property should be type: identifier with value: baz
{
	"type": "Object",
	"object": {
		"type": "Object",
		"object": {
			"type": "Identifier",
			"value": "foo"
		},
		"property": {
			"type": "String",
			"value": "a"
		}
	},
	"property": {
		"type": "Object",
		"object": {
			"type": "Identifier",
			"value": "bar"
		},
		"property": {
			"type": "Identifier",
			"value": "baz"
		}
	}
}

Add parser errors

About

Abstract Syntac Tree Parser for javascript

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published