var nearley = require("nearley");
var grammar = require("./grammar.js");
var tokenizer = require("./tokenizer");
var lineColumn = require("line-column");
var excerptAtLineCol = require("excerpt-at-line-col");

var mkParseError = function(src, line, col, orig_err, filename){
    var msg = "" + orig_err;
    msg = msg.trim();

    if(/Error: invalid syntax at/.test(orig_err)){
        msg = "No possible parsings";
    }

    msg += "\n" + (filename  || "") + ":" + line + ":" + col;

    msg += "\n \n" + excerptAtLineCol(src, line - 1, col - 1, 0);

    var err = new Error(msg);
    err.where = {
        filename: filename,
        line: line,
        col: col,
        excerpt: excerptAtLineCol(src, line - 1, col - 1, 3)
    };
    return err;
};

module.exports = function(src, opts){
    opts = opts || {};

    var tokens = tokenizer(src).filter(function(t){
        return true
            && t.type !== "WHITESPACE"
            && t.type !== "LINE-COMMENT"
            && t.type !== "BLOCK-COMMENT"
            ;
    });

    var p = new nearley.Parser(grammar.ParserRules, grammar.ParserStart);
    try{
        p.feed(tokens);
    }catch(e){
        if(typeof e.offset === "number" && tokens[e.offset] && tokens[e.offset].loc){
            var lc = lineColumn(src, tokens[e.offset].loc.start);
            if(lc){
                throw mkParseError(src, lc.line, lc.col, e, opts.filename);
            }
        }
        throw e;
    }
    if(p.results.length !== 1){
        throw new Error(
            "Parsing Ambiguity: " + p.results.length + " parsings found"
        );
    }
    return p.results[0];
};

@@ -1488,36 +1488,17 @@
     
-throw new Error(%0A           
+var msg =
  %22Pa
@@ -1554,16 +1554,17 @@
 s found%22
+;
 %0A       
@@ -1564,17 +1564,207 @@
         
-)
+if(opts.filename)%7B%0A            msg += %22%5Cn%22 + opts.filename;%0A        %7D%0A        var err = new Error(msg);%0A        err.where = %7B%0A            filename: opts.filename,%0A        %7D;%0A        throw err
 ;%0A    %7D%0A

include filename on Ambiguity errors