lib/standard/string: Moved escape_to_dot from nitcc to standard/string.nit
[nit.git] / contrib / nitcc / src / grammar.nit
index 4ecefc3..620b0a0 100644 (file)
@@ -27,14 +27,15 @@ class Gram
        # Dump of the concrete grammar and the transformations
        fun pretty: String
        do
-               var res = new Buffer
+               var res = new FlatBuffer
                for p in prods do
                        if p.spe != null then
                                res.append("{p.name} \{-> {p.spe.name}\}=\n")
                        else
                                res.append("{p.name} =\n")
                        end
-                       var last = p.alts.last
+                       var last = null
+                       if not p.alts.is_empty then p.alts.last
                        for a in p.alts do
                                res.append("\t\{{a.name}:\} {a.elems.join(" ")}")
                                if a.codes == null then a.make_codes
@@ -60,6 +61,7 @@ class Gram
        do
                for p in self.prods do
                        for a in p.alts.to_a do
+                               if a.phony then continue
                                var to_inline = false
                                for e in a.elems do
                                        if e isa Production and prods.has(e) then to_inline = true
@@ -87,6 +89,7 @@ class Gram
                                        end
                                        pool2.clear
                                        for a2 in e.alts do
+                                               if a.phony then continue
                                                if a2.codes == null then a2.make_codes
                                                for x in pool do
                                                        var name = a.name + "_" + pool2.length.to_s
@@ -206,6 +209,7 @@ class Gram
                        for p in prods do
                                if p.is_nullable then continue
                                for a in p.alts do
+                                       if a.phony then continue
                                        var nullabl = true
                                        for e in a.elems do
                                                if e isa Token then
@@ -232,6 +236,7 @@ class Gram
                        for p in prods do
                                var fs = p.firsts
                                for a in p.alts do
+                                       if a.phony then continue
                                        var i = a.first_item
                                        loop
                                                var e = i.next
@@ -258,6 +263,7 @@ class Gram
                        var changed = false
                        for p1 in prods do
                                for a in p1.alts do
+                                       if a.phony then continue
                                        var p0: nullable Production = null
                                        var i = a.first_item
                                        loop
@@ -324,11 +330,11 @@ class Production
 
        # Is self transformed to a other production for the AST
        # FIXME: cleaup AST
-       var spe: nullable Production writable = null
+       var spe: nullable Production = null is writable
 
        # Is self contains only a single alternative (then no need for a abstract production class in the AST)
        # FIXME cleanup AST
-       var altone writable = false
+       var altone = false is writable
 
        # The cname of the class in the AST
        # FIXME: cleanup AST
@@ -374,6 +380,7 @@ class Production
        do
                var res = new Array[Item]
                for a in alts do
+                       if a.phony then continue
                        res.add a.first_item
                end
                return res
@@ -389,7 +396,7 @@ class Alternative
        var prod: Production
 
        # The name of the alternative
-       var name: String writable
+       var name: String is writable
 
        # The elements of the alternative
        var elems: Array[Element]
@@ -417,10 +424,13 @@ class Alternative
        end
 
        # The code for the reduction
-       var codes: nullable Array[Code] writable = null
+       var codes: nullable Array[Code] = null is writable
 
        # Is the alternative transformed (ie not in the AST)
-       var trans writable = false
+       var trans = false is writable
+
+       # Is the alternative unparsable? (ie not in the automaton)
+       var phony = false is writable
 
        # Imitialize codes with the elements
        fun make_codes
@@ -610,14 +620,6 @@ class LRAutomaton
        end
 end
 
-redef class String
-       # escape string used in labels for graphviz
-       fun escape_to_dot: String
-       do
-               return escape_more_to_c("|\{\}")
-       end
-end
-
 private class Generator
        var out = new Array[String]
        fun add(s: String) do out.add(s)
@@ -988,31 +990,41 @@ class LRState
                                print "REDUCE/REDUCE Conflict on state {self.number} {self.name} for token {t}:"
                                for i in a do print "\treduce: {i}"
                        else if guarded_shift.has_key(t) then
-                               for ri in a.to_a do for si in guarded_shift[t] do
-                                       if lookahead(ri).has(si) then
-                                               var p = ri.alt.prod
-                                               var csi: nullable Item = null
-                                               for bsi in back_expand(si) do
-                                                       if bsi.alt.prod == p then
-                                                               csi = bsi
-                                                               break
-                                                       end
-                                               end
-                                               if csi == null then continue
-                                               print "Automatic Dangling on state {self.number} {self.name} for token {t}:"
-                                               print "\treduce: {ri}"
-                                               print "\tshift:  {si}"
-                                               if si != csi then
-                                                       print "\tcore shift: {csi}"
+                               var ri = a.first
+                               var confs = new Array[Item]
+                               var ress = new Array[String]
+                               var g = guarded_shift[t]
+                               for si in lookahead(ri) do
+                                       if si.next != t then continue
+                                       if not g.has(si) then
+                                               confs.add(si)
+                                               continue
+                                       end
+                                       var p = ri.alt.prod
+                                       var csi: nullable Item = null
+                                       for bsi in back_expand(si) do
+                                               if bsi.alt.prod == p then
+                                                       csi = bsi
+                                                       break
                                                end
-                                               a.remove(ri)
+                                       end
+                                       if csi == null then
+                                               confs.add(si)
+                                               continue
+                                       end
+                                       ress.add "\tshift:  {si}"
+                                       if si != csi then
+                                               ress.add "\tcore shift: {csi}"
                                        end
                                end
-                               if a.is_empty then
+                               if confs.is_empty then
+                                       print "Automatic Dangling on state {self.number} {self.name} for token {t}:"
+                                       print "\treduce: {ri}"
+                                       for r in ress do print r
                                        guarded_reduce.keys.remove(t)
                                else
                                        print "SHIFT/REDUCE Conflict on state {self.number} {self.name} for token {t}:"
-                                       for i in a do print "\treduce: {i}"
+                                       print "\treduce: {ri}"
                                        for i in guarded_shift[t] do print "\tshift:  {i}"
                                end
                        end
@@ -1062,7 +1074,7 @@ class Item
 
        redef fun to_s
        do
-               var b = new Buffer
+               var b = new FlatBuffer
                b.append("{alt.prod.name}::{alt.name}=")
                for i in [0..alt.elems.length[
                do