Skip to content
This repository has been archived by the owner on Jan 20, 2023. It is now read-only.

Commit

Permalink
feat: add custom properties grammar
Browse files Browse the repository at this point in the history
  • Loading branch information
ldez committed Apr 24, 2016
1 parent 5f995f8 commit abb1fe4
Show file tree
Hide file tree
Showing 4 changed files with 236 additions and 22 deletions.
12 changes: 6 additions & 6 deletions grammars/asciidoc.cson
Original file line number Diff line number Diff line change
Expand Up @@ -926,9 +926,9 @@
'end': '^\\1*$'
'endCaptures':
'0': 'name': 'support.asciidoc'
'name': 'markup.code.git-config.asciidoc'
'contentName': 'source.embedded.git-config'
'patterns': ['include': 'source.git-config']
'name': 'markup.code.properties.asciidoc'
'contentName': 'source.embedded.asciidoc.properties'
'patterns': ['include': 'source.asciidoc.properties']
}
]
}
Expand Down Expand Up @@ -1911,9 +1911,9 @@
'end': '^\\s*\\1\\s*$'
'endCaptures':
'0': 'name': 'support.asciidoc'
'name': 'markup.code.git-config.asciidoc'
'contentName': 'source.embedded.git-config'
'patterns': ['include': 'source.git-config']
'name': 'markup.code.properties.asciidoc'
'contentName': 'source.embedded.asciidoc.properties'
'patterns': ['include': 'source.asciidoc.properties']
}

# Matches Makefile Markdown-style code blocks
Expand Down
95 changes: 95 additions & 0 deletions grammars/properties-lang.cson
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
# Hack for fix grammar parsing bug (`java-properties`, `git-config`)
name: 'Hack for properties'
scopeName: 'source.asciidoc.properties'
fileTypes: []
patterns: [
{
include: '#section'
}
{
include: '#value_pair'
}
{
include: '#comment'
}
]
repository:
# Comment grammar
comment:
match: '((#|;).*$\\n?)'
captures:
1:
name: 'comment.line.number-sign.asciidoc.properties'
2:
name: 'punctuation.definition.comment.asciidoc.properties'
3:
name: 'comment.line.semi-colon.asciidoc.properties'
4:
name: 'punctuation.definition.comment.asciidoc.properties'
# Section header grammar
section:
match: '^\\[\\s*([\\w_-]+)(?:\\s+((")(?:[^"\\\\]|^\\\\["\\\\])*("))|\\.([\\w_-]+))?\\s*\\]'
name: 'meta.section.asciidoc.properties'
captures:
1:
name: 'entity.name.section.asciidoc.properties'
2:
name: 'entity.name.section.subsection.asciidoc.properties'
3:
name: 'punctuation.definition.section.subsection.begin.asciidoc.properties'
4:
name: 'punctuation.definition.section.subsection.end.asciidoc.properties'
5:
name: 'entity.name.section.subsection.asciidoc.properties'
# Value pair
'value_pair':
name: 'meta.value-pair.section-item.asciidoc.properties'
begin: '([-\\w]+)\\s*(=)\\s*(?!$)'
captures:
1:
name: 'support.constant.asciidoc.properties'
2:
name: 'punctuation.separator.key-value.asciidoc.properties'
end: '$|(?=[#;])'
patterns: [
{
include: '#boolean'
}
{
include: '#escaped-string'
}
{
include: '#string'
}
{
include: '#comment'
}
]
# Boolean value
boolean:
match: '\\b(?i:yes|no|0|1|true|false)\\b'
name: 'constant.language.boolean.asciidoc.properties'
# String value
string:
name: 'string.quoted.double.asciidoc.properties'
begin: '"'
beginCaptures:
0:
name: 'punctuation.definition.string.begin.asciidoc.properties'
end: '"'
endCaptures:
0:
name: 'punctuation.definition.string.end.asciidoc.properties'
patterns: [
{
match: '\\\\[ntb"\\\\]'
name: 'constant.character.escape.asciidoc.properties'
}
{
match: '\\\\.'
name: 'invalid.illegal.unknown-escape.asciidoc.properties'
}
]
'escaped-string':
match: '\\\\"'
name: 'constant.character.escape.asciidoc.properties'
52 changes: 36 additions & 16 deletions spec/fixtures/asciidoctor-lang.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -445,22 +445,42 @@ COPY /tmp /tmp
----


== Properties *BUG !*

// ```properties
// # comment
// [user]
// name = John Doe
// email = example@examplecom
// ```

// [source,properties]
// ----
// # comment
// [user]
// name = John Doe
// email = example@examplecom
// ----
== Properties

```properties
# comment
foo = foo-{0}-bar\"foo\"
; comment
[user]
name = John Doe ; comment
email = example@examplecom # comment
[section-foo "riooeri"]
key = \"foobar\" ; comment
key = "foobar" # comment
[section.foo]
key = \"foobar\" ; comment
key = "foobar" # comment
[alias]
lg = log --graph --pretty=tformat:'%Cred%h%Creset %Cblue%G?%Creset -%C(auto)%d%Creset %s %Cgreen(%an %ar)%Creset'
```

[source,properties]
----
# comment
foo = foo-{0}-bar\"foo\"
; comment
[user]
name = John Doe ; comment
email = example@examplecom # comment
[section-foo "riooeri"]
key = \"foobar\" ; comment
key = "foobar" # comment
[section.foo]
key = \"foobar\" ; comment
key = "foobar" # comment
[alias]
lg = log --graph --pretty=tformat:'%Cred%h%Creset %Cblue%G?%Creset -%C(auto)%d%Creset %s %Cgreen(%an %ar)%Creset'
----


== Makefile
Expand Down
99 changes: 99 additions & 0 deletions spec/properties-lang-spec.coffee
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
describe 'Properties grammar', ->
grammar = null

beforeEach ->
waitsForPromise ->
atom.packages.activatePackage 'language-asciidoc'

runs ->
grammar = atom.grammars.grammarForScopeName 'source.asciidoc.properties'

# convenience function during development
debug = (tokens) ->
console.log(JSON.stringify(tokens, null, ' '))

it 'load the "properties" config grammar', ->
expect(grammar).toBeTruthy()
expect(grammar.scopeName).toBe 'source.asciidoc.properties'

describe 'Should parse "section" when', ->
it 'was a simple word', ->
{tokens} = grammar.tokenizeLine '[foobar]'
expect(tokens).toHaveLength 3
expect(tokens[0]).toEqual value: '[', scopes: ['source.asciidoc.properties', 'meta.section.asciidoc.properties']
expect(tokens[1]).toEqual value: 'foobar', scopes: ['source.asciidoc.properties', 'meta.section.asciidoc.properties', 'entity.name.section.asciidoc.properties']
expect(tokens[2]).toEqual value: ']', scopes: ['source.asciidoc.properties', 'meta.section.asciidoc.properties']

it 'have a subsection separate by a dot', ->
{tokens} = grammar.tokenizeLine '[foo.bar]'
expect(tokens).toHaveLength 5
expect(tokens[0]).toEqual value: '[', scopes: ['source.asciidoc.properties', 'meta.section.asciidoc.properties']
expect(tokens[1]).toEqual value: 'foo', scopes: ['source.asciidoc.properties', 'meta.section.asciidoc.properties', 'entity.name.section.asciidoc.properties']
expect(tokens[2]).toEqual value: '.', scopes: ['source.asciidoc.properties', 'meta.section.asciidoc.properties']
expect(tokens[3]).toEqual value: 'bar', scopes: ['source.asciidoc.properties', 'meta.section.asciidoc.properties', 'entity.name.section.subsection.asciidoc.properties']
expect(tokens[4]).toEqual value: ']', scopes: ['source.asciidoc.properties', 'meta.section.asciidoc.properties']

it 'have a subsection with double-quote', ->
{tokens} = grammar.tokenizeLine '[foo "bar"]'
expect(tokens).toHaveLength 7
expect(tokens[0]).toEqual value: '[', scopes: ['source.asciidoc.properties', 'meta.section.asciidoc.properties']
expect(tokens[1]).toEqual value: 'foo', scopes: ['source.asciidoc.properties', 'meta.section.asciidoc.properties', 'entity.name.section.asciidoc.properties']
expect(tokens[2]).toEqual value: ' ', scopes: ['source.asciidoc.properties', 'meta.section.asciidoc.properties']
expect(tokens[3]).toEqual value: '"', scopes: ['source.asciidoc.properties', 'meta.section.asciidoc.properties', 'entity.name.section.subsection.asciidoc.properties', 'punctuation.definition.section.subsection.begin.asciidoc.properties']
expect(tokens[4]).toEqual value: 'bar', scopes: ['source.asciidoc.properties', 'meta.section.asciidoc.properties', 'entity.name.section.subsection.asciidoc.properties']
expect(tokens[5]).toEqual value: '"', scopes: ['source.asciidoc.properties', 'meta.section.asciidoc.properties', 'entity.name.section.subsection.asciidoc.properties', 'punctuation.definition.section.subsection.end.asciidoc.properties']
expect(tokens[6]).toEqual value: ']', scopes: ['source.asciidoc.properties', 'meta.section.asciidoc.properties']

describe 'Should parse "comment" when', ->
it 'start by a hash', ->
{tokens} = grammar.tokenizeLine '# comment'
expect(tokens).toHaveLength 2
expect(tokens[0]).toEqual value: '#', scopes: ['source.asciidoc.properties', 'comment.line.number-sign.asciidoc.properties', 'punctuation.definition.comment.asciidoc.properties']
expect(tokens[1]).toEqual value: ' comment', scopes: ['source.asciidoc.properties', 'comment.line.number-sign.asciidoc.properties']

it 'start by a ;', ->
{tokens} = grammar.tokenizeLine '; comment'
expect(tokens).toHaveLength 2
expect(tokens[0]).toEqual value: ';', scopes: ['source.asciidoc.properties', 'comment.line.number-sign.asciidoc.properties', 'punctuation.definition.comment.asciidoc.properties']
expect(tokens[1]).toEqual value: ' comment', scopes: ['source.asciidoc.properties', 'comment.line.number-sign.asciidoc.properties']

describe 'Should parse "value pair" when', ->
it 'have a simple value', ->
{tokens} = grammar.tokenizeLine 'name = foobar'
expect(tokens).toHaveLength 5
expect(tokens[0]).toEqual value: 'name', scopes: ['source.asciidoc.properties', 'meta.value-pair.section-item.asciidoc.properties', 'support.constant.asciidoc.properties']
expect(tokens[1]).toEqual value: ' ', scopes: ['source.asciidoc.properties', 'meta.value-pair.section-item.asciidoc.properties']
expect(tokens[2]).toEqual value: '=', scopes: ['source.asciidoc.properties', 'meta.value-pair.section-item.asciidoc.properties', 'punctuation.separator.key-value.asciidoc.properties']
expect(tokens[3]).toEqual value: ' ', scopes: ['source.asciidoc.properties', 'meta.value-pair.section-item.asciidoc.properties']
expect(tokens[4]).toEqual value: 'foobar', scopes: ['source.asciidoc.properties', 'meta.value-pair.section-item.asciidoc.properties']

it 'have a value embedded in double quote', ->
{tokens} = grammar.tokenizeLine 'name = "foobar"'
expect(tokens).toHaveLength 7
expect(tokens[0]).toEqual value: 'name', scopes: ['source.asciidoc.properties', 'meta.value-pair.section-item.asciidoc.properties', 'support.constant.asciidoc.properties']
expect(tokens[1]).toEqual value: ' ', scopes: ['source.asciidoc.properties', 'meta.value-pair.section-item.asciidoc.properties']
expect(tokens[2]).toEqual value: '=', scopes: ['source.asciidoc.properties', 'meta.value-pair.section-item.asciidoc.properties', 'punctuation.separator.key-value.asciidoc.properties']
expect(tokens[3]).toEqual value: ' ', scopes: ['source.asciidoc.properties', 'meta.value-pair.section-item.asciidoc.properties']
expect(tokens[4]).toEqual value: '"', scopes: ['source.asciidoc.properties', 'meta.value-pair.section-item.asciidoc.properties', 'string.quoted.double.asciidoc.properties', 'punctuation.definition.string.begin.asciidoc.properties']
expect(tokens[5]).toEqual value: 'foobar', scopes: ['source.asciidoc.properties', 'meta.value-pair.section-item.asciidoc.properties', 'string.quoted.double.asciidoc.properties']
expect(tokens[6]).toEqual value: '"', scopes: ['source.asciidoc.properties', 'meta.value-pair.section-item.asciidoc.properties', 'string.quoted.double.asciidoc.properties', 'punctuation.definition.string.end.asciidoc.properties']

it 'have a boolean value', ->
{tokens} = grammar.tokenizeLine 'name = true'
expect(tokens).toHaveLength 5
expect(tokens[0]).toEqual value: 'name', scopes: ['source.asciidoc.properties', 'meta.value-pair.section-item.asciidoc.properties', 'support.constant.asciidoc.properties']
expect(tokens[1]).toEqual value: ' ', scopes: ['source.asciidoc.properties', 'meta.value-pair.section-item.asciidoc.properties']
expect(tokens[2]).toEqual value: '=', scopes: ['source.asciidoc.properties', 'meta.value-pair.section-item.asciidoc.properties', 'punctuation.separator.key-value.asciidoc.properties']
expect(tokens[3]).toEqual value: ' ', scopes: ['source.asciidoc.properties', 'meta.value-pair.section-item.asciidoc.properties']
expect(tokens[4]).toEqual value: 'true', scopes: ['source.asciidoc.properties', 'meta.value-pair.section-item.asciidoc.properties', 'constant.language.boolean.asciidoc.properties']

it 'have a value contains escaped double quote', ->
{tokens} = grammar.tokenizeLine 'name = \\"foobar\\"'
expect(tokens).toHaveLength 7
expect(tokens[0]).toEqual value: 'name', scopes: ['source.asciidoc.properties', 'meta.value-pair.section-item.asciidoc.properties', 'support.constant.asciidoc.properties']
expect(tokens[1]).toEqual value: ' ', scopes: ['source.asciidoc.properties', 'meta.value-pair.section-item.asciidoc.properties']
expect(tokens[2]).toEqual value: '=', scopes: ['source.asciidoc.properties', 'meta.value-pair.section-item.asciidoc.properties', 'punctuation.separator.key-value.asciidoc.properties']
expect(tokens[3]).toEqual value: ' ', scopes: ['source.asciidoc.properties', 'meta.value-pair.section-item.asciidoc.properties']
expect(tokens[4]).toEqual value: '\\"', scopes: ['source.asciidoc.properties', 'meta.value-pair.section-item.asciidoc.properties', 'constant.character.escape.asciidoc.properties']
expect(tokens[5]).toEqual value: 'foobar', scopes: ['source.asciidoc.properties', 'meta.value-pair.section-item.asciidoc.properties']
expect(tokens[6]).toEqual value: '\\"', scopes: ['source.asciidoc.properties', 'meta.value-pair.section-item.asciidoc.properties', 'constant.character.escape.asciidoc.properties']

0 comments on commit abb1fe4

Please sign in to comment.