Skip to content

Instantly share code, notes, and snippets.

@chomwitt
Created December 20, 2012 11:13
Show Gist options
  • Save chomwitt/4344731 to your computer and use it in GitHub Desktop.
Save chomwitt/4344731 to your computer and use it in GitHub Desktop.
<!DOCTYPE HTML>
<html>
<head>
<title>WebTurtle/Parser tests</title>
<meta charset="utf-8">
<script language="javascript" SRC="./js/peg-0.7.0.js"></script>
<script language="peg" id="grammar">
start
= additive
additive
= left:multiplicative "+" right:additive { return left + right; }
/ multiplicative
multiplicative
= left:primary "*" right:multiplicative { return left * right; }
/ primary
primary
= integer
/ "(" additive:additive ")" { return additive; }
integer "integer"
= digits:[0-9]+ { return parseInt(digits.join(""), 10); }
</script>
</head>
<body>
<script>
var parser = PEG.buildParser(document.getElementById("grammar").innerText);
result = parser.parse("5 + 6");
document.write("<h1>Out with the old - in with the new!</h1>" );
document.write(result);
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment