foo123 / codemirror-grammar

Transform a JSON grammar into a syntax-highlight parser for CodeMirror
https://foo123.github.io/examples/codemirror-grammar/
80 stars 8 forks source link

code completion #7

Open danieldroit opened 6 years ago

danieldroit commented 6 years ago

I have the following grammar:

{
  "RegExpID": "RE::",
  "Style": {
    "string": "string",
    "this": "keyword",
    "operator": "operator",
    "regex": "string-2",
    "atom": "atom",
    "identifier": "variable",
    "keyword": "keyword",
    "number": "number",
    "property": "attribute",
    "date": "string-2",
    "comment": "comment",
    "builtin": "builtin"
  },
  "Lex": {
    "boolean": {
      "autocomplete": true,
      "tokens": [
        "true",
        "false"
      ]
    },
    "string": {
      "type": "escaped-block",
      "escape": "\\",
      "tokens": [
        "RE::/(['\"])/",
        1
      ]
    },
    "this": "RE::/this\\b/",
    "operator": {
      "tokens": [
        "+",
        "-",
        "++",
        "--",
        "%",
        ">>",
        "<<",
        ">>>",
        "*",
        "/",
        "^",
        "|",
        "&",
        "!",
        "~",
        ">",
        "<",
        "<=",
        ">=",
        "!=",
        "!==",
        "=",
        "==",
        "===",
        "+=",
        "-=",
        "%=",
        ">>=",
        ">>>=",
        "<<=",
        "*=",
        "/=",
        "|=",
        "&="
      ]
    },
    "regex": {
      "type": "escaped-block",
      "escape": "\\",
      "tokens": [
        "/",
        "RE::#/[gimy]{0,4}#"
      ]
    },
    "atom": {
      "autocomplete": true,
      "tokens": [
        "null",
        "undefined",
        "NaN",
        "Infinity"
      ]
    },
    "delimiter": {
      "tokens": [
        "(",
        ")",
        "[",
        "]",
        "{",
        "}",
        ",",
        "=",
        ";",
        "?",
        " ",
        "+=",
        "-=",
        "*=",
        "/=",
        "%=",
        "&=",
        "|=",
        "^=",
        "++",
        "--",
        ">>=",
        "<<="
      ]
    },
    "identifier": "RE::/[_A-Za-z$][_A-Za-z0-9$]*/",
    "keyword": {
      "autocomplete": true,
      "tokens": [
        "if",
        "while",
        "with",
        "else",
        "do",
        "try",
        "finally",
        "return",
        "break",
        "continue",
        "new",
        "delete",
        "throw",
        "var",
        "const",
        "let",
        "function",
        "catch",
        "void",
        "for",
        "switch",
        "case",
        "default",
        "class",
        "import",
        "yield",
        "in",
        "typeof",
        "instanceof"
      ]
    },
    "number": [
      "RE::/\\d*\\.\\d+(e[\\+\\-]?\\d+)?/",
      "RE::/\\d+\\.\\d*/",
      "RE::/\\.\\d+/",
      "RE::/0x[0-9a-fA-F]+L?/",
      "RE::/0b[01]+L?/",
      "RE::/0o[0-7]+L?/",
      "RE::/[1-9]\\d*(e[\\+\\-]?\\d+)?L?/",
      "RE::/0(?![\\dx])/"
    ],
    "property": "RE::/[_A-Za-z$][_A-Za-z0-9$]*/",
    "date": "RE::/\\d{4}-\\d{2}-\\d{2}$]*/",
    "comment": {
      "type": "comment",
      "tokens": [
        [
          ";",
          "null"
        ],
        [
          "/*",
          "*/"
        ]
      ]
    },
    "builtin": {
      "autocomplete": true,
      "tokens": [
        "Object",
        "Function",
        "Array",
        "String",
        "Date",
        "Number",
        "RegExp",
        "Math",
        "Exception",
        "setTimeout",
        "setInterval",
        "parseInt",
        "parseFloat",
        "isFinite",
        "isNan",
        "alert",
        "prompt",
        "console",
        "window",
        "global",
        "this"
      ]
    }
  },
  "Syntax": {
    "json": "object | array | prim",
    "object": "'{' kv (',' kv)* '}'",
    "js": "comment | number | string | regex | keyword | operator | atom | (('}' | ')' | this | builtin | identifier | dot_property) dot_property*)",
    "kv": {
      "sequence": [
        "string",
        "':'",
        "json"
      ]
    },
    "top": "query",
    "prim": "number | string | boolean",
    "query": "'{' '\"query\"'    ':' string ',' '\"where\"' ':' criteria '}'",
    "operand": "identifier | date | string | number | boolean",
    "operator2": "'\"=\"' | '\">\"' | '\">=\"' | '\"<\"' | '\"<=\"'",
    "or" :          "\"or\" (',' criteria)+",
    "and":          "\"and\" (',' criteria)+",
    "criteria": "'[' (or | and | relation)    ']'",
    "dot_property": {
      "sequence": [
        ".",
        "property"
      ]
    },
    "array": "'[' json (',' json)* ']'",
    "relation": "'[' operator2 ',' operand ',' operand ']'"
  },
  "Parser": [
    [
      "top"
    ]
  ]
}

When I paste it to the grammar editor on http://foo123.github.io/examples/codemirror-grammar/ and clean the code editor the Ctrl+SPACE suggest true | false which is not valid language according to grammar as it must start with {. Furthermore, if I type { and then Ctrl+SPACE it correctly suggests "query" but when I subsequently use Ctrl+SPACE instead of : or : " it suggests "where" which again results in incorrect syntax.

Please advise whether it is a bug or I'm doing something wrong. Many thanks for your work.

foo123 commented 6 years ago

If i am not mistaken, the tokens '{', '}', ':' are NOT marked as tokens for autocompletion, so they are skipped in the autocomplete suggest functionality. if you necessarily need these to be autocompleted try to insert them as separete tokens in LEX and mark them as autocomplete. Let me know how it goes.