Skip to content

Instantly share code, notes, and snippets.

@DamnedScholar
Last active January 25, 2023 20:14
  • Star 50 You must be signed in to star a gist
  • Fork 5 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save DamnedScholar/622926bcd222eb1ddc483d12103fd315 to your computer and use it in GitHub Desktop.
Grammar boilerplate with annotations.
# TextMate tutorial: http://manual.macromates.com/en/language_grammars
# Regex to convert keys to unquoted: '(include|match|captures|begin|end|beginCaptures|endCaptures|name|patterns|0|1|2|3|4|5|6|7|8|9|comment|fileTypes|scopeName|repository|contentName|firstLineMatch|foldingStartMarker|foldingStopMarker)':
scopeName: 'source.<scope>' # <scope> should be a short, unique indicator for the language ("js", "php", "c", etc.)
name: '<name>' # The title that will show up in grammar selection and on your status bar.
fileTypes: [ # An array of file extensions.
'txt'
'exif'
]
firstLineMatch: '' # A regular expression that is matched against the first line of the document when Atom is trying to decide if the grammar is appropriate. Useful for shell scripts, mostly.
foldingStartMarker: '' # A regular expression that checks for the start of a foldable area (such as `{`).
foldingStopMarker: '' # A regular expression that checks for the end of a foldable area (such as `}`). If both folding markers are matched in the same line, there will be no foldable area.
patterns: [ # An array of individual pattern declarations.
{
match: '' # A regular expression that matches some content. Optional.
begin: '' # A regular expression that precedes the content. Optional.
end: '' # A regular expression that follows the content. Optional.
name: '' # A period-delimited list of classes that will be applied to the content identified by `match`. Optional.
contentName: '' # A period-delimited list of classes that will be applied to the content within `begin` and `end`. Optional.
captures: # An array of `name` designations for `match` expressions that capture multiple bits. Optional.
0: # Arrays start at zero. This one will catch the whole regex. 1+ will catch the capture groups.
name: '' # This should be self-explanatory by now.
beginCaptures: # An array of `name` designations for `begin` expressions that capture multiple bits. Optional.
0:
name: ''
endCaptures: # An array of `name` designations for `end` expressions that capture multiple bits. Optional.
0:
name: ''
include: '' # This can be a reference to another grammar (by scope name), the current grammar (with `$self`), or a rule from the grammar's repository (with `#<rule>`). Optional.
patterns: [] # This is where you can get tricky. When you have matched content, Atom will search within it for anything in the child array. You can have `patterns[]` within `patterns[]`, then throw an `include` in to jump to some other rule or refer back to the root of the grammar. If you are making a rule that captures a block that might contain other tags, you should refer back to the root. Optional, naturally.
}
{
match: ''
begin: ''
end: ''
name: ''
contentName: ''
captures:
0:
name: ''
beginCaptures:
0:
name: ''
endCaptures:
0:
name: ''
include: ''
}
]
repository: # An array of rules. If you're going to be referencing one bit of code over and over again, stick it here so that you can edit all in one place.
ruleName: # Can be anything.
{
name: ''
match: ''
end: ''
endCaptures:
0:
name: ''
}
@Aerijo
Copy link

Aerijo commented Jan 30, 2018

@DamnedScholar (don't know why I bothered; @mentions don't work in gists...)

Anyway, it's probably worth noting Atom doesn't support foldingStartMarker and foldingStopMarker at all right now. This may change in the future, so there is no harm in adding the properties now, but people expecting it to work should be aware of this.

Only indentation folding is supported in core. Community packages may add dedicated support to a language though.

Issue is atom/first-mate#48

@zerox1212
Copy link

Is there an example of how to implement basic IF ELIF THEN ELSE style syntax checking? I can't find a simple example to make a grammar for simple control structures.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment