/* Copyright (c) 2012-2017 The ANTLR Project. All rights reserved.
 * Use of this file is governed by the BSD 3-clause license that
 * can be found in the LICENSE.txt file in the project root.
 */

// A DFA walker that knows how to dump them to serialized strings.#/


class DFASerializer {
    constructor(dfa, literalNames, symbolicNames) {
        this.dfa = dfa;
        this.literalNames = literalNames || [];
        this.symbolicNames = symbolicNames || [];
    }

    toString() {
       if(this.dfa.s0 === null) {
           return null;
       }
       var buf = "";
       var states = this.dfa.sortedStates();
       for(var i=0;i<states.length;i++) {
           var s = states[i];
           if(s.edges!==null) {
                var n = s.edges.length;
                for(var j=0;j<n;j++) {
                    var t = s.edges[j] || null;
                    if(t!==null && t.stateNumber !== 0x7FFFFFFF) {
                        buf = buf.concat(this.getStateString(s));
                        buf = buf.concat("-");
                        buf = buf.concat(this.getEdgeLabel(j));
                        buf = buf.concat("->");
                        buf = buf.concat(this.getStateString(t));
                        buf = buf.concat('\n');
                    }
                }
           }
       }
       return buf.length===0 ? null : buf;
    }

    getEdgeLabel(i) {
        if (i===0) {
            return "EOF";
        } else if(this.literalNames !==null || this.symbolicNames!==null) {
            return this.literalNames[i-1] || this.symbolicNames[i-1];
        } else {
            return String.fromCharCode(i-1);
        }
    }

    getStateString(s) {
        var baseStateStr = ( s.isAcceptState ? ":" : "") + "s" + s.stateNumber + ( s.requiresFullContext ? "^" : "");
        if(s.isAcceptState) {
            if (s.predicates !== null) {
                return baseStateStr + "=>" + s.predicates.toString();
            } else {
                return baseStateStr + "=>" + s.prediction.toString();
            }
        } else {
            return baseStateStr;
        }
    }
}

class LexerDFASerializer extends DFASerializer {
    constructor(dfa) {
        super(dfa, null);
        return this;
    }

    getEdgeLabel(i) {
        return "'" + String.fromCharCode(i) + "'";
    }
}


exports.DFASerializer = DFASerializer;
exports.LexerDFASerializer = LexerDFASerializer;


@@ -556,19 +556,19 @@
 %0A       
-var
+let
  buf = %22
@@ -577,19 +577,21 @@
 %0A       
-var
+const
  states 
@@ -632,16 +632,17 @@
 for(
-var
+let
  i=0;
+ 
 i%3Cst
@@ -653,16 +653,17 @@
 .length;
+ 
 i++) %7B%0A 
@@ -672,19 +672,21 @@
         
-var
+const
  s = sta
@@ -741,19 +741,21 @@
         
-var
+const
  n = s.e
@@ -787,19 +787,21 @@
     for(
-var
+const
  j=0;j%3Cn
@@ -828,19 +828,21 @@
         
-var
+const
  t = s.e
@@ -1706,11 +1706,13 @@
     
-var
+const
  bas
@@ -2231,29 +2231,8 @@
 l);%0A
-        return this;%0A
     
@@ -2320,76 +2320,62 @@
 %0A%7D%0A%0A
-%0Aexports.DFASerializer = DFASerializer;%0Aexports.LexerDFASerializer =
+module.exports = %7B DFASerializer %7D;%0Amodule.exports = %7B
  Lex
@@ -2389,11 +2389,13 @@
 rializer
+ %7D
 ;%0A%0A

use const/let use module exports instead of exports