| 1 |
local info = { |
| 2 |
version = 1.002, |
| 3 |
comment = "scintilla lpeg lexer for bibtex", |
| 4 |
author = "Hans Hagen, PRAGMA-ADE, Hasselt NL", |
| 5 |
copyright = "PRAGMA ADE / ConTeXt Development Team", |
| 6 |
license = "see context related readme files", |
| 7 |
} |
| 8 |
|
| 9 |
local global, string, table, lpeg = _G, string, table, lpeg |
| 10 |
local P, R, S, V = lpeg.P, lpeg.R, lpeg.S, lpeg.V |
| 11 |
local type = type |
| 12 |
|
| 13 |
local lexers = require("scite-context-lexer") |
| 14 |
|
| 15 |
local patterns = lexers.patterns |
| 16 |
local token = lexers.token |
| 17 |
|
| 18 |
local bibtexlexer = lexers.new("bib","scite-context-lexer-bibtex") |
| 19 |
local bibtexwhitespace = bibtexlexer.whitespace |
| 20 |
|
| 21 |
local escape, left, right = P("\\"), P('{'), P('}') |
| 22 |
|
| 23 |
patterns.balanced = P { |
| 24 |
[1] = ((escape * (left+right)) + (1 - (left+right)) + V(2))^0, |
| 25 |
[2] = left * V(1) * right |
| 26 |
} |
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
local anything = patterns.anything |
| 31 |
local percent = P("%") |
| 32 |
local start = P("@") |
| 33 |
local comma = P(",") |
| 34 |
local hash = P("#") |
| 35 |
local escape = P("\\") |
| 36 |
local single = P("'") |
| 37 |
local double = P('"') |
| 38 |
local left = P('{') |
| 39 |
local right = P('}') |
| 40 |
local lineending = S("\n\r") |
| 41 |
local space = S(" \t\n\r\f") |
| 42 |
local spaces = space^1 |
| 43 |
local equal = P("=") |
| 44 |
|
| 45 |
local keyword = (R("az","AZ","09") + S("@_:-"))^1 |
| 46 |
|
| 47 |
|
| 48 |
local s_quoted = ((escape*single) + (1-single))^0 |
| 49 |
local d_quoted = ((escape*double) + (1-double))^0 |
| 50 |
|
| 51 |
local balanced = patterns.balanced |
| 52 |
|
| 53 |
local t_spacing = token(bibtexwhitespace,space^1) |
| 54 |
local t_optionalws = token("default",space^1)^0 |
| 55 |
|
| 56 |
local t_equal = token("operator",equal) |
| 57 |
local t_left = token("grouping",left) |
| 58 |
local t_right = token("grouping",right) |
| 59 |
local t_comma = token("operator",comma) |
| 60 |
local t_hash = token("operator",hash) |
| 61 |
|
| 62 |
local t_s_value = token("operator",single) |
| 63 |
* token("text",s_quoted) |
| 64 |
* token("operator",single) |
| 65 |
local t_d_value = token("operator",double) |
| 66 |
* token("text",d_quoted) |
| 67 |
* token("operator",double) |
| 68 |
local t_b_value = token("operator",left) |
| 69 |
* token("text",balanced) |
| 70 |
* token("operator",right) |
| 71 |
local t_r_value = token("text",keyword) |
| 72 |
|
| 73 |
local t_keyword = token("keyword",keyword) |
| 74 |
local t_key = token("command",keyword) |
| 75 |
local t_label = token("warning",keyword) |
| 76 |
|
| 77 |
local t_somevalue = t_s_value + t_d_value + t_b_value + t_r_value |
| 78 |
local t_value = t_somevalue |
| 79 |
* ((t_optionalws * t_hash * t_optionalws) * t_somevalue)^0 |
| 80 |
|
| 81 |
local t_assignment = t_optionalws |
| 82 |
* t_key |
| 83 |
* t_optionalws |
| 84 |
* t_equal |
| 85 |
* t_optionalws |
| 86 |
* t_value |
| 87 |
|
| 88 |
local t_shortcut = t_keyword |
| 89 |
* t_optionalws |
| 90 |
* t_left |
| 91 |
* t_optionalws |
| 92 |
* (t_assignment * t_comma^0)^0 |
| 93 |
* t_optionalws |
| 94 |
* t_right |
| 95 |
|
| 96 |
local t_definition = t_keyword |
| 97 |
* t_optionalws |
| 98 |
* t_left |
| 99 |
* t_optionalws |
| 100 |
* t_label |
| 101 |
* t_optionalws |
| 102 |
* t_comma |
| 103 |
* (t_assignment * t_comma^0)^0 |
| 104 |
* t_optionalws |
| 105 |
* t_right |
| 106 |
|
| 107 |
local t_comment = t_keyword |
| 108 |
* t_optionalws |
| 109 |
* t_left |
| 110 |
* token("text",(1-t_right)^0) |
| 111 |
* t_optionalws |
| 112 |
* t_right |
| 113 |
|
| 114 |
local t_forget = token("comment",percent^1 * (1-lineending)^0) |
| 115 |
|
| 116 |
local t_rest = token("default",anything) |
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
local dummylexer = lexers.load("scite-context-lexer-dummy","bib-dum") |
| 129 |
|
| 130 |
local dummystart = token("embedded",P("\001")) |
| 131 |
local dummystop = token("embedded",P("\002")) |
| 132 |
|
| 133 |
lexers.embed(bibtexlexer,dummylexer,dummystart,dummystop) |
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
bibtexlexer.rules = { |
| 140 |
{ "whitespace", t_spacing }, |
| 141 |
{ "forget", t_forget }, |
| 142 |
{ "shortcut", t_shortcut }, |
| 143 |
{ "definition", t_definition }, |
| 144 |
{ "comment", t_comment }, |
| 145 |
{ "rest", t_rest }, |
| 146 |
} |
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
bibtexlexer.folding = { |
| 179 |
["{"] = { ["grouping"] = 1 }, |
| 180 |
["}"] = { ["grouping"] = -1 }, |
| 181 |
} |
| 182 |
|
| 183 |
return bibtexlexer |