- return result;
- };
- let space = (x) => {
- let wrapped_x = toSome(x);
- let s_aux = orDo(match1Char(' '), match1Char('\t')); // (" " | "\t")
- var result = thenDo(thenDo(wrapped_x, s_aux), zeroOrMoreDo(s_aux));
- if (result._tag == "Some") {
- result.value.matched_type = TokenType.SP;
- }
- return result;
- };
- let newline = (x) => {
- let wrapped_x = toSome(x);
- // nl = \r?\n
- let result = thenDo(thenDo(wrapped_x, zeroOrOnceDo(match1Char('\r'))), match1Char('\n'));
- if (result._tag == "Some") {
- result.value.matched_type = TokenType.NL;
+ let new_ast = [ast_middle];
+ a.value.ast = new_ast;
+ }
+ return a;
+};
+/** single1 = tInt | "(" expr ")"*/
+let single1 = circumfix((x) => thenDo(thenDo(thenDo(toSome(x), tLParen), expr), tRParen), "fac1");
+let single2 = tInt;
+let single = orDo(single1, single2);
+/** args = single "," args | single */
+let args1 = (x) => {
+ var ret = thenDo(thenDo(thenDo(toSome(x), single), tComma), args);
+ if (ret._tag == "Some") {
+ let retLength = ret.value.ast.length;
+ ret.value.ast = [[ret.value.ast[retLength - 3]].concat(ret.value.ast[retLength - 1])];
+ console.log("$$" + repr(ret.value.ast));
+ }
+ return ret;
+};
+let args2 = single;
+let args = orDo(args1, args2);
+/** callees = "(" args ")" | "(" ")" */
+let callees1 = circumfix((x) => thenDo(thenDo(thenDo(toSome(x), tLParen), args), tRParen), "callees1");
+let callees2 = (x) => {
+ let ret = thenDo(thenDo(toSome(x), tLParen), tRParen);
+ if (ret._tag == "Some") {
+ let new_ast = [[]];
+ ret.value.ast = new_ast;
+ }
+ return ret;
+};
+let callees = orDo(callees1, callees2);
+/** %apply R combinating token */
+let applyToken = {
+ text: "%apply",
+ type: tk.TokenType.ID,
+ col: 0,
+ ln: 0,
+};
+/** facAux = callees facAux | callees */
+let facAux1 = (x) => {
+ var ret = thenDo(thenDo(toSome(x), callees), facAux);
+ if (ret._tag == "Some") {
+ console.log("1232345" + repr(tkTreeToSExp(ret.value.ast[ret.value.ast.length - 1])));
+ let last1 = ret.value.ast[ret.value.ast.length - 1];
+ let last2 = ret.value.ast[ret.value.ast.length - 2];
+ let b = [applyToken];
+ ret.value.ast = [b.concat([last2, last1])];
+ console.log("11111" + repr(tkTreeToSExp(ret.value.ast)));
+ }
+ ;
+ return ret;
+};
+let facAux2 = callees;
+let facAux = orDo(facAux1, facAux2);
+/** fac = single facAux | single
+ * Issue1 to be fixed.
+ */
+let fac1 = (x) => {
+ var ret = thenDo(thenDo(toSome(x), single), facAux);
+ if (ret._tag == "Some") {
+ console.log("777" + repr(tkTreeToSExp(ret.value.ast)));
+ ret.value.ast = [applyToken, ret.value.ast[ret.value.ast.length - 2],
+ ret.value.ast[ret.value.ast.length - 1]];
+ ret.value.ast;
+ rearrangeTree(ret.value.ast);
+ console.log("888" + repr(tkTreeToSExp(ret.value.ast)));
+ }
+ return ret;
+};
+let fac2 = single;
+let fac = orDo(fac1, fac2);
+/**
+ * rearrangeTree : for applyToken subtree from right-combination to
+ * left-combination
+ * @input x a ast
+ * @return another ast
+ */
+function rearrangeTree(x) {
+ if (x !== undefined) {
+ for (var i = 1; i < x.length; i++) {
+ rearrangeTree(x[i]);