1

I have a function inside a string:

def sum(num):

end

And I want to convert it to:

function sum(num) {
}

using jison. I am using the grammar defined in a javascript object:

const grammar = {
"lex": {
    "macros": {
        "function":     "def ",
        "name_function":"(_*[a-zA-Z][a-zA-Z0-9_]*)\\(",
        "arguments_function": "(\\s*_*[a-zA-Z][a-zA-Z0-9_]*\\s*,?)*\\):"
    },
    "rules": [
        ["{",                   "return '{'"],
        [":",                   "return ':'"],
        ["}",                   "return '}'"],
        ["end",                 "return '}'"],
        ['\\/\\/.*',            "/* ignore comment */"],
        ["\\s+",                "/* skip whitespace */"],
        ["{function}",          "return 'FUNCTION'"],
        ["{name_function}",     "return 'NAME_FUNCTION'"],
        ["{arguments_function}","return 'ARGS_FUNCTION'"],

        ["$",                   "return 'EOF'"]
    ]
},

"operators": [
    ["left", "UMINUS"]
],

"bnf": {
    "expressions": [
        ["e EOF",   "return $1"]
    ],

    "e" :[
        ["FUNCTION NAME_FUNCTION ARGS_FUNCTION :",  "$$ = 'function ' + $NAME_FUNCTION.trim() + $ARGS_FUNCTION.trim() + '{';"],
    ["}",  "$$ = '}';"]
    ]
}
};

module.exports = grammar;

I do not find the error in the defined regular expressions, or I do not know if it is a jison error.

Thanks.

anlijudavid
  • 509
  • 6
  • 12

0 Answers0