Help creating a grammar from scratch


#1

I have read the other posts and after all that I am still lacking traction.

What I am trying to do is create a grammar file for Trainz Game Script if someone is willing to help who knows the way to have the files so that I then add the various keywords etc that would be wonderfully accepted.

Thanks,
Larry


#2

Have you read the section in the Flight Manual? I have a variety of small grammar packages that I’ve whipped up, and you can check out one of those as a model. You don’t need anything except for a CSON/JSON file in grammars/ and a package.json. The easiest way is to start from the package-generator (Packages -> Package Generator -> Generate Atom Package).


#3

Yes I did read (as said) and I used package generator, some of the other samples et al but it is not working, and worst from my point of view when you install a plugin even in dev it doesn’t tell you that there are errors it just doesn’t work which is pretty unfriendly.

My almost empty package is on github


#4

You said that you read posts about it, on the forum. You did not say that you had read the Flight Manual.

I used package generator, some of the other samples et al but it is not working

Your grammar works just fine (see below). What part of the process seems to be failing for you?


#5

It is not highlighting, it is not in the grammar list … your example has trainz.cson that’s not my file name it is trainz-script.cson


#6

Ah, got it. You have a grammers/ folder, but no grammars/ folder.

That’s because I have an init.coffee script for loading in the active editor as a grammar. It makes testing changes go much faster.

atom.commands.add 'atom-text-editor', 'custom:load-active-editor-as-grammar', ->
    editor = atom.workspace.getActiveTextEditor()
    atom.grammars.loadGrammarSync(editor.getPath())

#7

Here’s another link you might like


#8

Apologies for being dense, browsed @Aerijo referred docs, added init.coffee @DamnedScholar no joy.

I have updated the github code with some sample code it is still highlighting .gs files as JavaScript and “Trainz Script” doesn’t appear when you try and set the grammar :sunglasses:


#9

I have refreshed everything and some things are working … let me get my axe out to hack some more :drooling_face:


#10

Some things? What is happening? What were you expecting? I’ve never tried DamnedScholar’s script, but I just reloaded the relevant window when I was developing a grammar. If you don’t have many community packages enabled, refreshing is quite quick.

Some other things:

  • The file name of the grammar doesn’t matter

  • This kind of grammar is going to be deprecated eventually anyway by tree-sitter grammars. They are already rolling out in the beta version of Atom. While support for regex based grammars will likely continue for some time, it’s worth asking yourself if you want to invest significant time developing a product that is being phased out anyway.

  • Errors are thrown when packages don’t work. I don’t know what makes you think otherwise. Have you checked the dev console?

  • Is the language a superset of another? If so, you’ll probably want to include: source.langName at the end of the patterns array.


#11

When I add include : source.js it takes over everything and although I am using different names for the language components they all appear only as purple.

Ug … I just want syntax high-lighting for the script … didn’t want to learn the internals of atom/tree-sitter/etc

It aint as though I have not done this before, Notepad++, ultraedit and a couple of others I wanted to switch to atom so that people doing GS can be on Mac/Windows/whatever and have syntax highlighting.

oh well


#12

Colour is determined by the theme, not the grammar. To see what the grammar is doing, use Editor: Log Cursor Scope in the command palette.

In your case, you are using the same scope for bool and void. So they will be the same colour. new is a different scope, so it’s colour may be different (depending on the current theme)


#13

So how do I tell the ones that are ‘keyword.control.gs’ to use the javascript break|case|catch… formatting?

If I can simply tell the thing that x|y|z are additional keywords in JS, a|b|c are additional types etc that will do the JS grammar is pretty good as the ‘base’ but has oodles of code that I didn’t want to copy&paste and hack as it would all blow up fer sher


#14

So you want something like this then?

scopeName: 'source.gs'
name: 'Trainz Game Script'
fileTypes: [
  'gs'
  'gsi'
  'gss'
]

injections:
  'L:*':
    patterns: [
        {
            match: 'void'
            name: 'storage.type.gs'
        }
        {
            match: 'bool'
            name: 'storage.type.gs'
        }
        {
            match: 'new'
            name: 'keyword.control.gs'
        }
    ]

patterns: [{ include: 'source.js' }]

This will use the JS grammar for most things, but will override it if an injections pattern matches. On that note, it was kind of important to tell us you were writing an extension of a language. The title implies you are writing one … from scratch. If this still doesn’t help, could you please give a detailed description of what you’re trying to do?


#15

You’re asking how to colour certain words the same colur as break, case, and catch?

If so, just use the scope they use (so you’d want keyword.control.gs).


#16

I am/was trying to do it from scratch, I tried using a number of the templates, C++ is probably closer to GS but it also has JS style stuff. I only started including JS as a result of this topic :grinning:

So how do I associate ‘storage.type.gs’ with the formatting for storage.type.js oh I suppose I am assuming there is one in all that code somewhere …


#17

There is no association of anything. As I said, the actual colours you see are not done by the grammar package. They are from the theme you are using, which looks at the scopes of each character and applies a determined colour per match (like CSS). If the theme doesn’t have a colour for storage.type or storage, it will not be coloured.

The .js / .gs ending of scopes is largely unused in this process. It is there for other reasons.


#18

If the grammar is a standalone one, you are (probably) better of making it from scratch in the long run. Maybe you can get an approximation of it using JS as a base, and maybe it will be half decent, but if it also shares C syntax then I have no idea how to make that work.


#19

Right so if I can track down what the ‘name.item.js’ is then if I put in ‘name.item.gs’ it will be formatted the same, ok will try and see if I can do the association(s) needed.

Thanks … you have got me to the stage where at least I can see my stuff colouring just need to get it like JS names mapping for consistency ie so that keywords end up looking the same.


#20

You should be following the scope styles given in the TextMate documentation the best you can. Sometimes (most of the time I find) a choice isn’t clear cut, so you’ll need to use your judgement in these cases. It’s always preferable to use scopes that other packages are using (excluding the language part at the end of each scope of course). For a good reference, look at one-dark-syntax for the scopes it applies colours to.