Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Where to find grammar for different languages ? #4

Open
Mayhul-Jindal opened this issue Nov 25, 2023 · 1 comment
Open

Where to find grammar for different languages ? #4

Mayhul-Jindal opened this issue Nov 25, 2023 · 1 comment

Comments

@Mayhul-Jindal
Copy link

Where can I find grammar for different languages(lets say golang) in the JSON format like you have in the grammar directory

@Skyost
Copy link

Skyost commented Dec 29, 2023

I've used ChatGPT to generate me a JSON file for Python and it worked pretty well. Here's the result, if you're interested.

{
  "name": "Python",
  "version": "1.0.0",
  "fileTypes": ["py"],
  "scopeName": "source.python",
  "foldingStartMarker": "\\b(?:def|class)\\s*[^:]*:\\s*$",
  "foldingStopMarker": "^\\s*\\}",
  "patterns": [
    {"include": "#comments"},
    {"include": "#keywords"},
    {"include": "#constants-and-special-vars"},
    {"include": "#operators"},
    {"include": "#strings"}
  ],
  "repository": {
    "comments": {
      "patterns": [
        {"name": "comment.line.hash.python", "match": "#.*$"},
        {"name": "comment.block.python", "begin": "'''", "end": "'''"},
        {"name": "comment.block.python", "begin": "\"\"\"", "end": "\"\"\""}
      ]
    },
    "keywords": {
      "patterns": [
        {"name": "keyword.control.python", "match": "\\b(?:if|else|while|for|in|break|continue|return)\\b"},
        {"name": "keyword.operator.logical.python", "match": "\\b(?:and|or|not)\\b"},
        {"name": "keyword.operator.assignment.python", "match": "="},
        {"name": "storage.modifier.python", "match": "\\b(?:def|class)\\b"}
      ]
    },
    "constants-and-special-vars": {
      "patterns": [
        {"name": "constant.language.python", "match": "\\b(?:True|False|None)\\b"},
        {"name": "variable.language.python", "match": "\\b(?:self)\\b"},
        {"name": "constant.numeric.python", "match": "\\b(?:\\d+\\.?\\d*|\\.\\d+)\\b"}
      ]
    },
    "operators": {
      "patterns": [
        {"name": "keyword.operator.arithmetic.python", "match": "\\b(?:\\+|-|\\*|/|%|//)\\b"},
        {"name": "keyword.operator.comparison.python", "match": "\\b(?:==|!=|<|<=|>|>=)\\b"},
        {"name": "keyword.operator.logical.python", "match": "\\b(?:and|or|not)\\b"}
      ]
    },
    "strings": {
      "patterns": [
        {"name": "string.quoted.triple.double.python", "begin": "\"\"\"", "end": "\"\"\""},
        {"name": "string.quoted.triple.single.python", "begin": "'''", "end": "'''"},
        {"name": "string.quoted.double.python", "begin": "\"", "end": "\"", "patterns": [{"include": "#string-escape"}]},
        {"name": "string.quoted.single.python", "begin": "'", "end": "'", "patterns": [{"include": "#string-escape"}]}
      ]
    },
    "string-escape": {
      "patterns": [
        {"name": "constant.character.escape.python", "match": "\\\\[\"']"}
      ]
    }
  }
}

I've loaded it using Highlighter.initialize(['../../../assets/languages/python']);. To use it :

Highlighter highlighter = Highlighter(
  language: '../../../assets/languages/python',
  theme: highlighterTheme,
);
Text.rich(highlighter.highlight(pythonCode));

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants