From 237eebbe1a1abe98f3c7af5e1e1115a51526e6e7 Mon Sep 17 00:00:00 2001 From: Alec Thomas Date: Sun, 24 Sep 2017 21:46:27 +1000 Subject: [PATCH] A naive YAML lexer. Does not try to be smart in anyway. Instead, just highlights what it can. Used this reference: https://learnxinyminutes.com/docs/yaml/ Fixes #13. --- lexers/yaml.go | 75 +++++++++++++++++++++++++++++++++++++++++++++++ styles/swapoff.go | 1 + 2 files changed, 76 insertions(+) create mode 100644 lexers/yaml.go diff --git a/lexers/yaml.go b/lexers/yaml.go new file mode 100644 index 0000000..5066374 --- /dev/null +++ b/lexers/yaml.go @@ -0,0 +1,75 @@ +package lexers + +import ( + . "github.com/alecthomas/chroma" // nolint +) + +// Prism.languages.yaml = { +// 'scalar': { +// pattern: /([\-:]\s*(![^\s]+)?[ \t]*[|>])[ \t]*(?:((?:\r?\n|\r)[ \t]+)[^\r\n]+(?:\3[^\r\n]+)*)/, +// lookbehind: true, +// alias: 'string' +// }, +// 'comment': /#.*/, +// 'key': { +// pattern: /(\s*(?:^|[:\-,[{\r\n?])[ \t]*(![^\s]+)?[ \t]*)[^\r\n{[\]},#\s]+?(?=\s*:\s)/, +// lookbehind: true, +// alias: 'atrule' +// }, +// 'directive': { +// pattern: /(^[ \t]*)%.+/m, +// lookbehind: true, +// alias: 'important' +// }, +// 'datetime': { +// pattern: /([:\-,[{]\s*(![^\s]+)?[ \t]*)(\d{4}-\d\d?-\d\d?([tT]|[ \t]+)\d\d?:\d{2}:\d{2}(\.\d*)?[ \t]*(Z|[-+]\d\d?(:\d{2})?)?|\d{4}-\d{2}-\d{2}|\d\d?:\d{2}(:\d{2}(\.\d*)?)?)(?=[ \t]*($|,|]|}))/m, +// lookbehind: true, +// alias: 'number' +// }, +// 'boolean': { +// pattern: /([:\-,[{]\s*(![^\s]+)?[ \t]*)(true|false)[ \t]*(?=$|,|]|})/im, +// lookbehind: true, +// alias: 'important' +// }, +// 'null': { +// pattern: /([:\-,[{]\s*(![^\s]+)?[ \t]*)(null|~)[ \t]*(?=$|,|]|})/im, +// lookbehind: true, +// alias: 'important' +// }, +// 'string': { +// pattern: /([:\-,[{]\s*(![^\s]+)?[ \t]*)("(?:[^"\\]|\\.)*"|'(?:[^'\\]|\\.)*')(?=[ \t]*($|,|]|}))/m, +// lookbehind: true, +// greedy: true +// }, +// 'number': { +// pattern: /([:\-,[{]\s*(![^\s]+)?[ \t]*)[+\-]?(0x[\da-f]+|0o[0-7]+|(\d+\.?\d*|\.?\d+)(e[\+\-]?\d+)?|\.inf|\.nan)[ \t]*(?=$|,|]|})/im, +// lookbehind: true +// }, +// 'tag': /![^\s]+/, +// 'important': /[&*][\w]+/, +// 'punctuation': /---|[:[\]{}\-,|>?]|\.\.\./ + +var YAML = Register(MustNewLexer( + &Config{ + Name: "YAML", + Aliases: []string{"yaml"}, + Filenames: []string{"*.yaml", "*.yml"}, + MimeTypes: []string{"text/x-yaml"}, + }, + Rules{ + "root": { + {`\s+`, Whitespace, nil}, + {`#.*`, Comment, nil}, + {`!![^\s]+`, CommentPreproc, nil}, + {`&[^\s]+`, CommentPreproc, nil}, + {`\*[^\s]+`, CommentPreproc, nil}, + {Words(``, `\b`, "true", "false", "null"), KeywordConstant, nil}, + {`"(?:\\.|[^"])+"`, StringDouble, nil}, + {`\d\d\d\d-\d\d-\d\d([T ]\d\d:\d\d:\d\d(\.\d+)?(Z|\s+[-+]\d+)?)?`, LiteralDate, nil}, + {`([>|])(\s+)((?:(?:.*?$)(?:[\n\r]*?\2)?)*)`, ByGroups(StringDoc, StringDoc, StringDoc), nil}, + {`[+\-]?(0x[\da-f]+|0o[0-7]+|(\d+\.?\d*|\.?\d+)(e[\+\-]?\d+)?|\.inf|\.nan)\b`, Number, nil}, + {`[?:,\[\]]`, Punctuation, nil}, + {`.`, Text, nil}, + }, + }, +)) diff --git a/styles/swapoff.go b/styles/swapoff.go index f4f941a..e173174 100644 --- a/styles/swapoff.go +++ b/styles/swapoff.go @@ -20,4 +20,5 @@ var SwapOff = Register(chroma.MustNewStyle("swapoff", map[chroma.TokenType]strin chroma.NameAttribute: "#ansiteal", chroma.NameVariable: "bold", chroma.Error: "#ansired", + chroma.LiteralDate: "bold #ansiyellow", }))