Skip to content

Commit

Permalink
Changed the keyword 'var' to 'const' or 'let'.
Browse files Browse the repository at this point in the history
  • Loading branch information
7131 committed Dec 7, 2021
1 parent ffa7086 commit 6c4585e
Show file tree
Hide file tree
Showing 5 changed files with 126 additions and 127 deletions.
34 changes: 17 additions & 17 deletions controller.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// Controller class
var Controller = function() {
const Controller = function() {
// fields
this._prev = "";
this._parser = new Parser(Grammar, Converter);
Expand Down Expand Up @@ -28,7 +28,7 @@ Controller.prototype = {
this._stopButton.addEventListener("click", this._stop.bind(this), false);

// JuggleMaster
var board = document.getElementById("board");
const board = document.getElementById("board");
board.width = board.clientWidth;
board.height = board.width;
this._jmj = new Jmj({ "canvas": board });
Expand All @@ -43,17 +43,17 @@ Controller.prototype = {
this._resultArea.innerHTML = "";

// lexical and syntax analyze
var result = this._parser.tokenize(this._prev);
if (result.tokens == null) {
this._setError("unknown character(s)", result.valid, result.invalid);
const lex = this._parser.tokenize(this._prev);
if (lex.tokens == null) {
this._setError("unknown character(s)", lex.valid, lex.invalid);
return;
}
result = this._parser.parse(result.tokens);
if (result.tree == null) {
this._setError("syntax error", result.valid, result.invalid);
const syntax = this._parser.parse(lex.tokens);
if (syntax.tree == null) {
this._setError("syntax error", syntax.valid, syntax.invalid);
return;
}
this._setResult(result.tree);
this._setResult(syntax.tree);
},

// "Start" button process
Expand All @@ -68,7 +68,7 @@ Controller.prototype = {
}

// start
var obj = { "siteswap": this._validator.pattern, "showSiteswap": false };
const obj = { "siteswap": this._validator.pattern, "showSiteswap": false };
if (!this._jmj.startJuggling(obj)) {
this._messageArea.innerHTML = "JuggleMaster error";
this._messageArea.className = "error";
Expand All @@ -89,9 +89,9 @@ Controller.prototype = {
}

// write to the DOM elements
var head = document.createElement("div");
var ok = document.createElement("div");
var ng = document.createElement("div");
const head = document.createElement("div");
const ok = document.createElement("div");
const ng = document.createElement("div");
head.innerHTML = title;
head.className = "error";
ok.innerHTML = valid;
Expand All @@ -104,7 +104,7 @@ Controller.prototype = {

// write the result string
"_setResult": function(tree) {
var head = document.createElement("div");
const head = document.createElement("div");
this._resultArea.appendChild(head);

// get the result
Expand All @@ -118,9 +118,9 @@ Controller.prototype = {

// siteswap
head.innerHTML = "Valid";
var balls = document.createElement("div");
var period = document.createElement("div");
var state = document.createElement("div");
const balls = document.createElement("div");
const period = document.createElement("div");
const state = document.createElement("div");
balls.innerHTML = "balls: " + this._validator.balls;
period.innerHTML = "period: " + this._validator.period;
state.innerHTML = "state: " + this._validator.state.join(" ");
Expand Down
4 changes: 2 additions & 2 deletions grammar.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// Grammar object
var Grammar = {
const Grammar = {

"flag": "i",

Expand Down Expand Up @@ -112,7 +112,7 @@ var Grammar = {
}

// Syntax converter
var Converter = {
const Converter = {

// Pattern ::= Async | Synch ;
"Pattern": function(tree) {
Expand Down
90 changes: 45 additions & 45 deletions parser.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// Token class
var Token = function(label) {
const Token = function(label) {
this.setPattern(label, "");
}

Expand All @@ -24,7 +24,7 @@ Token.prototype = {
}

// Syntax tree class
var Tree = function(label, text) {
const Tree = function(label, text) {
if (label == null) {
this.label = "";
} else {
Expand All @@ -39,7 +39,7 @@ var Tree = function(label, text) {
}

// State stack class
var StateStack = function() {
const StateStack = function() {
this._stack = [];
}

Expand All @@ -48,24 +48,24 @@ StateStack.prototype = {

// push a state pair to the stack top
"push": function(tree, state) {
var pair = { "tree": tree, "state": state };
const pair = { "tree": tree, "state": state };
this._stack.push(pair);
},

// pop a state pair from the stack top, remove it, and return the tree
"popTree": function() {
var last = this._stack.length - 1;
const last = this._stack.length - 1;
if (last < 0) {
return null;
} else {
var pair = this._stack.pop();
const pair = this._stack.pop();
return pair.tree;
}
},

// peek the state number of the stack top
"peekState": function() {
var last = this._stack.length - 1;
const last = this._stack.length - 1;
if (last < 0) {
return 0;
} else {
Expand All @@ -81,42 +81,42 @@ StateStack.prototype = {
}

// Syntax parser class
var Parser = function(grammar, converter) {
const Parser = function(grammar, converter) {
// terminal symbols
var terms = grammar.terminals.concat(grammar.dummies);
const terms = grammar.terminals.concat(grammar.dummies);
this._terminals = terms.map(this._quoteSingle);
this._dummies = grammar.dummies.map(this._quoteSingle);

// lexical analysis elements
this._elements = [];
for (var i = 0; i < terms.length; i++) {
for (let i = 0; i < terms.length; i++) {
this._elements.push(new RegExp("^(" + terms[i] + ")", grammar.flag));
}

// production rules
this._rules = [];
var nonterms = [];
for (var i = 0; i < grammar.rules.length; i++) {
var pair = grammar.rules[i].split("=");
var symbol = pair[0];
const nonterms = [];
for (let i = 0; i < grammar.rules.length; i++) {
const pair = grammar.rules[i].split("=");
const symbol = pair[0];
this._rules.push({ "symbol": symbol, "count": parseInt(pair[1], 10) });
if (0 < i && nonterms.indexOf(symbol) < 0) {
// non-terminal symbols
nonterms.push(symbol);
}
}
nonterms.unshift("$");
var symbols = grammar.terminals.map(this._quoteSingle).concat(nonterms);
const symbols = grammar.terminals.map(this._quoteSingle).concat(nonterms);

// parsing table
this._table = [];
for (var i = 0; i < grammar.table.length; i++) {
var row = {};
for (var j = 0; j < symbols.length; j++) {
var match = grammar.table[i][j].match(/^(s|r|g)([0-9]+)$/);
for (let i = 0; i < grammar.table.length; i++) {
const row = {};
for (let j = 0; j < symbols.length; j++) {
const match = grammar.table[i][j].match(/^(s|r|g)([0-9]+)$/);
if (match) {
var symbol = match[1];
var number = parseInt(match[2], 10);
const symbol = match[1];
const number = parseInt(match[2], 10);
row[symbols[j]] = { "symbol": symbol, "number": number };
}
}
Expand All @@ -130,11 +130,11 @@ Parser.prototype = {

// lexical analysis
"tokenize": function(text) {
var tokens = [];
const tokens = [];
while (0 < text.length) {
var max = new Token();
for (var i = 0; i < this._elements.length; i++) {
var result = this._elements[i].exec(text);
const max = new Token();
for (let i = 0; i < this._elements.length; i++) {
const result = this._elements[i].exec(text);
if (result != null && max.length < result[0].length) {
// get the longest and the first token
max.setPattern(this._terminals[i], result[0]);
Expand All @@ -154,38 +154,38 @@ Parser.prototype = {

// get the result
if (0 < text.length) {
var valid = tokens.reduce(this._joinTokens, "");
const valid = tokens.reduce(this._joinTokens, "");
return { "tokens": null, "valid": valid.trim(), "invalid": text };
}
return { "tokens": tokens };
},

// syntactic analysis
"parse": function(tokens) {
var stack = new StateStack();
const stack = new StateStack();

// dealing all tokens
tokens.push(new Token("$"));
while (0 < tokens.length) {
var next = tokens[0];
var label = next.label;
const next = tokens[0];
const label = next.label;

// execute an action
var action = this._table[stack.peekState()][label];
const action = this._table[stack.peekState()][label];
if (!action) {
break;
}
if (action.symbol == "s") {
// shift
var leaf = new Tree(label, next.text);
const leaf = new Tree(label, next.text);
stack.push(leaf, action.number);
tokens.shift();
} else {
// reduce
var rule = this._rules[action.number];
var nodes = [];
for (var i = 0; i < rule.count; i++) {
var top = stack.popTree();
const rule = this._rules[action.number];
const nodes = [];
for (let i = 0; i < rule.count; i++) {
const top = stack.popTree();
if (top.label.charAt(0) == "#") {
// a non-terminal symbol that should be removed
Array.prototype.unshift.apply(nodes, top.children);
Expand All @@ -195,7 +195,7 @@ Parser.prototype = {
}

// create a syntax tree
var node = new Tree(rule.symbol);
const node = new Tree(rule.symbol);
Array.prototype.push.apply(node.children, nodes);
if (this._converter[node.label]) {
this._converter[node.label](node);
Expand All @@ -207,27 +207,27 @@ Parser.prototype = {
}

// transit
action = this._table[stack.peekState()][node.label];
if (!action) {
const goto = this._table[stack.peekState()][node.label];
if (!goto) {
break;
}
stack.push(node, action.number);
stack.push(node, goto.number);
}
}

// the case of not to accept
var valid = "";
let valid = "";
while (0 < stack.getCount()) {
var tree = stack.popTree();
const tree = stack.popTree();
valid = this._joinTree(tree) + " " + valid;
}
var invalid = tokens.reduce(this._joinTokens, "");
const invalid = tokens.reduce(this._joinTokens, "");
return { "tree": null, "valid": valid.trim(), "invalid": invalid.trim() };
},

// add the single quatations
"_quoteSingle": function(cur) {
var text = cur.replace(/\\(.)/g, "$1");
const text = cur.replace(/\\(.)/g, "$1");
return "'" + text + "'";
},

Expand All @@ -243,8 +243,8 @@ Parser.prototype = {
}

// join all child elements
var text = "";
for (var i = 0; i < tree.children.length; i++) {
let text = "";
for (let i = 0; i < tree.children.length; i++) {
text += " " + this._joinTree(tree.children[i]);
}
return text;
Expand Down
Loading

0 comments on commit 6c4585e

Please sign in to comment.