|
| 1 | +/*! |
| 2 | + * sqlite-parser |
| 3 | + * @copyright Code School 2015 {@link http://codeschool.com} |
| 4 | + * @author Nick Wronski <nick@javascript.com> |
| 5 | + */ |
| 6 | +var parserUtils = require('./parser-util'); |
| 7 | + |
| 8 | +module.exports = (function (util) { |
| 9 | + Tracer = function Tracer() { |
| 10 | + if (!(this instanceof Tracer)) { |
| 11 | + return new Tracer(); |
| 12 | + } |
| 13 | + this.events = []; |
| 14 | + this.indentation = 0; |
| 15 | + this.whitespaceRule = /(^whitespace)|(char$)|(^[oe]$)/i; |
| 16 | + this.statementRule = /Statement$/i; |
| 17 | + this.firstNodeRule = /(Statement|Clause)$/i; |
| 18 | + }; |
| 19 | + |
| 20 | + Tracer.prototype.trace = function trace(event) { |
| 21 | + var that = this, lastIndex, lastWsIndex; |
| 22 | + event.indentation = this.indentation; |
| 23 | + switch (event.type) { |
| 24 | + case 'rule.enter': |
| 25 | + // add entered leaf |
| 26 | + this.events.push(event); |
| 27 | + this.indentation += 1; |
| 28 | + break; |
| 29 | + case 'rule.match': |
| 30 | + this.indentation -= 1; |
| 31 | + break; |
| 32 | + case 'rule.fail': |
| 33 | + // remove failed leaf |
| 34 | + lastIndex = util.findLastIndex(this.events, {rule: event.rule}); |
| 35 | + lastWsIndex = util.findLastIndex(this.events, function (e) { |
| 36 | + return !that.whitespaceRule.test(e.rule); |
| 37 | + }); |
| 38 | + if (that.whitespaceRule.test(event.rule) || lastIndex === lastWsIndex) { |
| 39 | + this.events.splice(lastIndex, 1); |
| 40 | + } |
| 41 | + this.indentation -= 1; |
| 42 | + break; |
| 43 | + } |
| 44 | + }; |
| 45 | + |
| 46 | + /** |
| 47 | + * @note |
| 48 | + * There is way too much magic/nonsense in this method now. Need to |
| 49 | + * come up with an alternative approach to getting the right |
| 50 | + * information for syntax errors. |
| 51 | + */ |
| 52 | + Tracer.prototype.smartError = function smartError(err) { |
| 53 | + var that = this, message, location, chain, chainDetail, firstNode, |
| 54 | + bestNode = {indentation: -1}, deep = false, stmts = 0, |
| 55 | + namedEvents = this.events |
| 56 | + .filter(function (e) { |
| 57 | + return e.description !== null && |
| 58 | + !that.whitespaceRule.test(e.rule); |
| 59 | + }) |
| 60 | + .reverse(); |
| 61 | + |
| 62 | + chain = util.takeWhile(namedEvents, function (elem) { |
| 63 | + if (/^(sym\_semi)$/i.test(elem.rule)) { |
| 64 | + stmts += 1; |
| 65 | + } |
| 66 | + if (stmts > 1) { |
| 67 | + return false; |
| 68 | + } |
| 69 | + if (!deep) { |
| 70 | + if (elem.indentation > bestNode.indentation) { |
| 71 | + bestNode = elem; |
| 72 | + } else { |
| 73 | + deep = true; |
| 74 | + } |
| 75 | + } else if (/^(stmt)$/i.test(elem.rule)) { |
| 76 | + deep = true; |
| 77 | + return true; |
| 78 | + } |
| 79 | + return true; |
| 80 | + }); |
| 81 | + |
| 82 | + if (chain.length) { |
| 83 | + location = bestNode.location; |
| 84 | + firstNode = util.findWhere(chain, function (elem) { |
| 85 | + return that.firstNodeRule.test(elem.description) && |
| 86 | + elem.description !== bestNode.description && |
| 87 | + elem.indentation !== bestNode.indentation; |
| 88 | + }); |
| 89 | + if (firstNode != null) { |
| 90 | + if (this.statementRule.test(bestNode.description) && |
| 91 | + this.statementRule.test(firstNode.description)) { |
| 92 | + chainDetail = firstNode.description; |
| 93 | + } else { |
| 94 | + chainDetail = bestNode.description + ' (' + firstNode.description + ')'; |
| 95 | + } |
| 96 | + } else { |
| 97 | + chainDetail = bestNode.description; |
| 98 | + } |
| 99 | + message = 'Syntax error found near ' + chainDetail; |
| 100 | + util.extend(err, { |
| 101 | + 'message': message, |
| 102 | + 'location': location |
| 103 | + }); |
| 104 | + } |
| 105 | + return err; |
| 106 | + }; |
| 107 | + |
| 108 | + return Tracer; |
| 109 | +})(parserUtils); |
0 commit comments