|
| 1 | +"use strict"; |
| 2 | + |
| 3 | +// Grammar implemented here: |
| 4 | +// bibtex -> (string | entry)*; |
| 5 | +// string -> '@STRING' kv_left key_equals_value kv_right; |
| 6 | +// entry -> '@' key kv_left key ',' key_value_list kv_right; |
| 7 | +// key_value_list -> key_equals_value (',' key_equals_value)* ','?; |
| 8 | +// key_equals_value -> key '=' value; |
| 9 | +// value -> value_quotes | value_braces | key; |
| 10 | +// value_quotes -> '"' .*? '"'; // not quite |
| 11 | +// value_braces -> '{' .*? '"'; // not quite |
| 12 | +// kv_left -> '(' | '{' |
| 13 | +// kv_right -> ')' | '}' |
| 14 | +function BibtexParser() { |
| 15 | + this._entries = {}; |
| 16 | + this._comments = []; |
| 17 | + this._strings = {}; |
| 18 | + this.input = ''; |
| 19 | + this.config = { |
| 20 | + upperKeys: false |
| 21 | + }; |
| 22 | + this._pos = 0; |
| 23 | + var pairs = { |
| 24 | + '{': '}', |
| 25 | + '(': ')', |
| 26 | + '"': '"' |
| 27 | + }; |
| 28 | + var regs = { |
| 29 | + atKey: /@([a-zA-Z0-9_:\\./-]+)\s*/, |
| 30 | + enLeft: /^([\{\(])\s*/, |
| 31 | + enRight: function enRight(left) { |
| 32 | + return new RegExp("^(\\".concat(pairs[left], ")\\s*")); |
| 33 | + }, |
| 34 | + entryId: /^\s*([^@={}",\s]+)\s*,\s*/, |
| 35 | + key: /^([a-zA-Z0-9_:\\./-]+)\s*=\s*/, |
| 36 | + vLeft: /^([\{"])\s*/, |
| 37 | + vRight: function vRight(left) { |
| 38 | + return new RegExp("^(\\".concat(pairs[left], ")\\s*")); |
| 39 | + }, |
| 40 | + inVLeft: /^(\{)\s*/, |
| 41 | + inVRight: function inVRight(left) { |
| 42 | + return new RegExp("^(\\".concat(pairs[left], ")\\s*")); |
| 43 | + }, |
| 44 | + value: /^[\{"]((?:[^\{\}]|\n)*?(?:(?:[^\{\}]|\n)*?\{(?:[^\{\}]|\n)*?\})*?(?:[^\{\}]|\n)*?)[\}"]\s*,?\s*/, |
| 45 | + word: /^([^\{\}"\s]+)\s*/, |
| 46 | + comma: /^(,)\s*/, |
| 47 | + quota: /^(")\s*/ |
| 48 | + }; |
| 49 | + |
| 50 | + this.setInput = function (t) { |
| 51 | + this.input = t; |
| 52 | + }; |
| 53 | + |
| 54 | + this.matchFirst = function (reg) { |
| 55 | + var notMove = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : false; |
| 56 | + var result = this.input.slice(this._pos).match(reg); |
| 57 | + |
| 58 | + if (result) { |
| 59 | + if (!notMove) { |
| 60 | + // console.log("!@#!@#", result[1]); |
| 61 | + this._pos += result.index + result[0].length; |
| 62 | + } |
| 63 | + |
| 64 | + return { |
| 65 | + success: true, |
| 66 | + text: result[1], |
| 67 | + index: result.index, |
| 68 | + step: result[0].length |
| 69 | + }; |
| 70 | + } else { |
| 71 | + return { |
| 72 | + success: false |
| 73 | + }; |
| 74 | + } |
| 75 | + }; |
| 76 | + |
| 77 | + this.assert = function (obj) { |
| 78 | + for (var key in obj) { |
| 79 | + if (obj[key] === undefined) { |
| 80 | + throw "[BibParser:ERROR] ".concat(key, " not found at ").concat(this._pos); |
| 81 | + } |
| 82 | + } |
| 83 | + }; |
| 84 | + |
| 85 | + this.getValue = function () { |
| 86 | + var stack = []; |
| 87 | + var values = []; |
| 88 | + |
| 89 | + var _this$matchFirst = this.matchFirst(regs.vLeft), |
| 90 | + vLeft = _this$matchFirst.text; |
| 91 | + |
| 92 | + this.assert({ |
| 93 | + vLeft: vLeft |
| 94 | + }); |
| 95 | + stack.push(vLeft); |
| 96 | + |
| 97 | + while (stack.length > 0) { |
| 98 | + if (this.matchFirst(regs.inVLeft, true).success) { |
| 99 | + var _this$matchFirst2 = this.matchFirst(regs.inVLeft), |
| 100 | + inVLeft = _this$matchFirst2.text; |
| 101 | + |
| 102 | + stack.push(inVLeft); |
| 103 | + values.push(inVLeft); |
| 104 | + } else if (this.matchFirst(regs.inVRight(stack[stack.length - 1]), true).success) { |
| 105 | + values.push(this.matchFirst(regs.inVRight(stack[stack.length - 1])).text); |
| 106 | + stack.pop(); |
| 107 | + } else if (this.matchFirst(regs.word, true).success) { |
| 108 | + values.push(this.matchFirst(regs.word).text); |
| 109 | + } else if (this.matchFirst(regs.quota, true).success) { |
| 110 | + values.push(this.matchFirst(regs.quota).text); |
| 111 | + } else { |
| 112 | + throw "[BibParser:ERROR] stack overflow at ".concat(this._pos); |
| 113 | + } |
| 114 | + } |
| 115 | + |
| 116 | + values.pop(); |
| 117 | + this.matchFirst(regs.comma); |
| 118 | + return values; |
| 119 | + }; |
| 120 | + |
| 121 | + this.string = function () { |
| 122 | + var _this$matchFirst3 = this.matchFirst(regs.key), |
| 123 | + key = _this$matchFirst3.text; |
| 124 | + |
| 125 | + this.assert({ |
| 126 | + key: key |
| 127 | + }); |
| 128 | + |
| 129 | + var _this$matchFirst4 = this.matchFirst(regs.value), |
| 130 | + value = _this$matchFirst4.text; |
| 131 | + |
| 132 | + this.assert({ |
| 133 | + value: value |
| 134 | + }); |
| 135 | + this._strings[key] = value; |
| 136 | + }; |
| 137 | + |
| 138 | + this.preamble = function () {}; |
| 139 | + |
| 140 | + this.comment = function () {}; |
| 141 | + |
| 142 | + this.entry = function (head) { |
| 143 | + var _this$matchFirst5 = this.matchFirst(regs.entryId), |
| 144 | + entryId = _this$matchFirst5.text; |
| 145 | + |
| 146 | + this.assert({ |
| 147 | + entryId: entryId |
| 148 | + }); |
| 149 | + var entry = {}; |
| 150 | + |
| 151 | + while (this.matchFirst(regs.key, true).success) { |
| 152 | + var _this$matchFirst6 = this.matchFirst(regs.key), |
| 153 | + key = _this$matchFirst6.text; |
| 154 | + |
| 155 | + var value = this.getValue(); |
| 156 | + entry[key] = value.join(' '); // if(key === 'author'){ |
| 157 | + // const {text:value} = this.matchFirst(regs.value); |
| 158 | + // this.assert({value}); |
| 159 | + // entry[key] = value; |
| 160 | + // } else { |
| 161 | + // const {text:value} = this.matchFirst(regs.value); |
| 162 | + // this.assert({value}); |
| 163 | + // entry[key] = value; |
| 164 | + // } |
| 165 | + } |
| 166 | + |
| 167 | + entry.$type = head; |
| 168 | + this._entries[entryId] = entry; |
| 169 | + }; |
| 170 | + |
| 171 | + this.parse = function () { |
| 172 | + while (this.matchFirst(regs.atKey, true).success) { |
| 173 | + var _this$matchFirst7 = this.matchFirst(regs.atKey), |
| 174 | + head = _this$matchFirst7.text; |
| 175 | + |
| 176 | + var _this$matchFirst8 = this.matchFirst(regs.enLeft), |
| 177 | + enLeft = _this$matchFirst8.text; |
| 178 | + |
| 179 | + this.assert({ |
| 180 | + enLeft: enLeft |
| 181 | + }); |
| 182 | + |
| 183 | + if (head.toUpperCase() == 'STRING') { |
| 184 | + this.string(); |
| 185 | + } else if (head.toUpperCase() == 'PREAMBLE') { |
| 186 | + this.preamble(); |
| 187 | + } else if (head.toUpperCase() == 'COMMENT') { |
| 188 | + this.comment(); |
| 189 | + } else { |
| 190 | + this.entry(head); |
| 191 | + } |
| 192 | + |
| 193 | + var _this$matchFirst9 = this.matchFirst(regs.enRight(enLeft)), |
| 194 | + enRight = _this$matchFirst9.text; |
| 195 | + |
| 196 | + this.assert({ |
| 197 | + enRight: enRight |
| 198 | + }); |
| 199 | + } |
| 200 | + }; |
| 201 | +} //Runs the parser |
| 202 | + |
| 203 | + |
| 204 | +export function bibParse(input) { |
| 205 | + var b = new BibtexParser(); |
| 206 | + b.setInput(input); |
| 207 | + b.parse(); |
| 208 | + return b._entries; |
| 209 | +} |
0 commit comments