Merge: parser: do not allocate a reduction table for each parser
[nit.git] / src / parser / xss / parser.xss
index 2f6d2db..5b1d60e 100644 (file)
-/* This file is part of NIT ( http://www.nitlanguage.org ).
- *
- * Copyright 2008 Jean Privat <jean@pryen.org>
- * Based on algorithms developped for ( http://www.sablecc.org/ ).
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- *     http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
+$ // This file is part of NIT ( http://www.nitlanguage.org ).
+$ //
+$ // Copyright 2008 Jean Privat <jean@pryen.org>
+$ // Based on algorithms developped for ( http://www.sablecc.org/ ).
+$ //
+$ // Licensed under the Apache License, Version 2.0 (the "License");
+$ // you may not use this file except in compliance with the License.
+$ // You may obtain a copy of the License at
+$ //
+$ //     http://www.apache.org/licenses/LICENSE-2.0
+$ //
+$ // Unless required by applicable law or agreed to in writing, software
+$ // distributed under the License is distributed on an "AS IS" BASIS,
+$ // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+$ // See the License for the specific language governing permissions and
+$ // limitations under the License.
 
 $ template make_parser()
 
-# State of the parser automata as stored in the parser stack.
-private class State
-       # The internal state number
-       readable writable attr _state: Int
-
-       # The node stored with the state in the stack
-       readable writable attr _nodes: Object 
-
-       init(state: Int, nodes: Object)
-       do
-               _state = state
-               _nodes = nodes
-       end
-end
-
 redef class Parser
-       # Associated lexer
-       attr _lexer: Lexer
-
-       # Stack of pushed states and productions
-       attr _stack: Array[State]
-
-       # Position in the stack
-       attr _stack_pos: Int
-
-       # Create a new parser based on a given lexer
-       init(lexer: Lexer)
+       redef fun build_reduce_table
        do
-               _lexer = lexer
-               _stack = new Array[State]
-               _stack_pos = -1
-               build_goto_table
-               build_action_table
-               build_reduce_table
-       end
-
-       # Do a transition in the automata
-       private meth go_to(index: Int): Int
-       do
-               var state = state
-               var table = _goto_table[index]
-               var low = 1
-               var high = table.length/2 - 1
-
-               while low <= high do
-                       var middle = (low + high) / 2
-                       var subindex = middle * 2
-
-                       if state < table[subindex] then
-                               high = middle - 1
-                       else if state > table[subindex] then
-                               low = middle + 1
-                       else
-                               return table[subindex + 1]
-                       end
-               end
-
-               return table[1] # Default value
-       end
-
-       # Push someting in the state stack
-       private meth push(numstate: Int, list_node: Object)
-       do
-               var pos = _stack_pos + 1
-               _stack_pos = pos
-               if pos < _stack.length then
-                       var state = _stack[pos]
-                       state.state = numstate
-                       state.nodes = list_node
-               else
-                       _stack.push(new State(numstate, list_node))
-               end
-       end
-
-       # The current state
-       private meth state: Int
-       do
-               return _stack[_stack_pos].state
-       end
-
-       # Pop something from the stack state
-       private meth pop: Object
-       do
-               var res = _stack[_stack_pos].nodes
-               _stack_pos = _stack_pos -1
-               return res
-       end
-
-       # Build and return a full AST.
-       meth parse: Start
-       do
-               push(0, null)
-
-               var ign: List[Token] = null
-               var lexer = _lexer
-               while true do
-                       var token = lexer.peek
-                       var last_pos = token.pos
-                       var last_line = token.line
-
-                       if token isa PError then
-                               assert token isa PError
-                               return new Start(null, token)
-                       end
-
-                       var index = token.parser_index
-                       var table = _action_table[state]
-                       var action_type = table[1]
-                       var action_value = table[2]
-
-                       var low = 1
-                       var high = table.length/3 - 1
-
-                       while low <= high do
-                               var middle = (low + high) / 2
-                               var subindex = middle * 3
-
-                               if index < table[subindex] then
-                                       high = middle - 1
-                               else if index > table[subindex] then
-                                       low = middle + 1
-                               else
-                                       action_type = table[subindex + 1]
-                                       action_value = table[subindex + 2]
-                                       high = low -1 # break
-                               end
-                       end
-
-                       if action_type == 0 then # SHIFT
-                               push(action_value, lexer.next)
-                       else if action_type == 1 then # REDUCE
-                               _reduce_table[action_value].action(self)
-                       else if action_type == 2 then # ACCEPT
-                               var node2 = lexer.next
-                               assert node2 isa EOF
-                               var node1 = pop
-                               assert node1 isa ${/parser/prods/prod/@ename}
-                               var node = new Start(node1, node2)
-                               (new SearchTokensVisitor).visit(node)
-                               return node
-                       else if action_type == 3 then # ERROR
-                               var node2 = new PError.init_error(lexer.filename, last_line, last_pos, error_messages[errors[action_value]])
-                               var node = new Start(null, node2)
-                               return node
-                       end
-               end
-               return null
-       end
-
-       attr _reduce_table: Array[ReduceAction]
-       private meth build_reduce_table
-       do
-               _reduce_table = new Array[ReduceAction].with(
+               var reduce_table = new Array[ReduceAction].with_capacity(${count(rules/rule)})
 $ foreach {rules/rule}
-                       new ReduceAction@index[-sep ','-]
+               reduce_table.add new ReduceAction@index(@leftside)
 $ end foreach
-               )
-       end
-end
-
-# Find first and last tokens of production nodes
-private class SearchTokensVisitor
-special Visitor
-       attr _untokenned_nodes: Array[Prod]
-       attr _last_token: Token
-       redef meth visit(n: PNode)
-       do
-               if n isa Token then
-                       assert n isa Token
-                       _last_token = n
-                       for no in _untokenned_nodes do
-                               no.first_token = n
-                       end
-                       _untokenned_nodes.clear
-               else
-                       assert n isa Prod
-                       _untokenned_nodes.add(n)
-                       n.visit_all(self)
-                       n.last_token = _last_token
-               end
+               return reduce_table
        end
-       init
-       do
-               _untokenned_nodes = new Array[Prod]
-       end
-end
-
-# Each reduca action has its own class, this one is the root of the hierarchy.
-private abstract class ReduceAction
-       meth action(p: Parser) is abstract
 end
 
 $ foreach {rules/rule}
 private class ReduceAction@index
-special ReduceAction
-       redef meth action(p: Parser)
+       super ReduceAction
+       redef fun action(p: Parser)
        do
-                                       var node_list: Object = null
+                                       var node_list: nullable Object = null
 $   foreach {action}
 $   choose
 $     when {@cmd='POP'}
                                        var ${translate(@result,"ABCDEFGHIJKLMNOPQRSTUVWXYZ","abcdefghijklmnopqrstuvwxyz")} = p.pop
 $     end
 $     when {@cmd='FETCHLIST'}
-                                       var ${translate(@result,"ABCDEFGHIJKLMNOPQRSTUVWXYZ","abcdefghijklmnopqrstuvwxyz")} = ${translate(@from,"ABCDEFGHIJKLMNOPQRSTUVWXYZ","abcdefghijklmnopqrstuvwxyz")} 
+                                       var ${translate(@result,"ABCDEFGHIJKLMNOPQRSTUVWXYZ","abcdefghijklmnopqrstuvwxyz")} = ${translate(@from,"ABCDEFGHIJKLMNOPQRSTUVWXYZ","abcdefghijklmnopqrstuvwxyz")}
                                        assert ${translate(@result,"ABCDEFGHIJKLMNOPQRSTUVWXYZ","abcdefghijklmnopqrstuvwxyz")} isa Array[Object]
 $     end
 $     when {@cmd='FETCHNODE'}
                                        var ${translate(@result,"ABCDEFGHIJKLMNOPQRSTUVWXYZ","abcdefghijklmnopqrstuvwxyz")} = ${translate(@from,"ABCDEFGHIJKLMNOPQRSTUVWXYZ","abcdefghijklmnopqrstuvwxyz")}
-                                       assert ${translate(@result,"ABCDEFGHIJKLMNOPQRSTUVWXYZ","abcdefghijklmnopqrstuvwxyz")} isa @etype
+                                       assert ${translate(@result,"ABCDEFGHIJKLMNOPQRSTUVWXYZ","abcdefghijklmnopqrstuvwxyz")} isa nullable @etype
 $     end
 $     when {@cmd='ADDNODE'}
                                        if ${translate(@node,"ABCDEFGHIJKLMNOPQRSTUVWXYZ","abcdefghijklmnopqrstuvwxyz")} != null then
@@ -235,19 +53,14 @@ $     when {@cmd='ADDNODE'}
                                        end
 $     end
 $     when {@cmd='ADDLIST'}
-                                       if ${translate(@fromlist,"ABCDEFGHIJKLMNOPQRSTUVWXYZ","abcdefghijklmnopqrstuvwxyz")} != null then
-                                               if ${translate(@tolist,"ABCDEFGHIJKLMNOPQRSTUVWXYZ","abcdefghijklmnopqrstuvwxyz")}.is_empty then
-                                                       ${translate(@tolist,"ABCDEFGHIJKLMNOPQRSTUVWXYZ","abcdefghijklmnopqrstuvwxyz")} = ${translate(@fromlist,"ABCDEFGHIJKLMNOPQRSTUVWXYZ","abcdefghijklmnopqrstuvwxyz")}
-                                               else
-                                                       ${translate(@tolist,"ABCDEFGHIJKLMNOPQRSTUVWXYZ","abcdefghijklmnopqrstuvwxyz")}.append(${translate(@fromlist,"ABCDEFGHIJKLMNOPQRSTUVWXYZ","abcdefghijklmnopqrstuvwxyz")})
-                                               end
-                                       end
+                                       ${translate(@tolist,"ABCDEFGHIJKLMNOPQRSTUVWXYZ","abcdefghijklmnopqrstuvwxyz")} = concat(${translate(@tolist,"ABCDEFGHIJKLMNOPQRSTUVWXYZ","abcdefghijklmnopqrstuvwxyz")}, ${translate(@fromlist,"ABCDEFGHIJKLMNOPQRSTUVWXYZ","abcdefghijklmnopqrstuvwxyz")})
 $     end
 $     when {@cmd='MAKELIST'}
                                        var ${translate(@result,"ABCDEFGHIJKLMNOPQRSTUVWXYZ","abcdefghijklmnopqrstuvwxyz")} = new Array[Object]
 $     end
 $     when {@cmd='MAKENODE'}
-                                       var ${translate(@result,"ABCDEFGHIJKLMNOPQRSTUVWXYZ","abcdefghijklmnopqrstuvwxyz")} = new @etype.init_${translate(@etype,"ABCDEFGHIJKLMNOPQRSTUVWXYZ","abcdefghijklmnopqrstuvwxyz")}(
+$      if {count(arg)!=0}
+                                       var ${translate(@result,"ABCDEFGHIJKLMNOPQRSTUVWXYZ","abcdefghijklmnopqrstuvwxyz")}: nullable @etype = new @etype.init_${translate(@etype,"ABCDEFGHIJKLMNOPQRSTUVWXYZ","abcdefghijklmnopqrstuvwxyz")}(
 $       foreach {arg}
 $           if @null
                                                null[-sep ','-]
@@ -256,6 +69,9 @@ $           else
 $           end
 $       end foreach
                                        )
+$      else
+                                       var ${translate(@result,"ABCDEFGHIJKLMNOPQRSTUVWXYZ","abcdefghijklmnopqrstuvwxyz")}: nullable @etype = new @etype.init_${translate(@etype,"ABCDEFGHIJKLMNOPQRSTUVWXYZ","abcdefghijklmnopqrstuvwxyz")}
+$      end
 $     end
 $     when {@cmd='RETURNNODE'}
 $       if @null
@@ -269,65 +85,40 @@ $     when {@cmd='RETURNLIST'}
 $     end
 $   end choose
 $   end foreach
-                                       p.push(p.go_to(@leftside), node_list)
+                                       p.push(p.go_to(_goto), node_list)
        end
-init do end
 end
 $ end foreach
 $ end template
 
-$ template make_parser_tables()
-# Parser that build a full AST
-class Parser
-       attr _action_table: Array[Array[Int]]
-       private meth build_action_table
-       do
-               _action_table = once [ 
+$ template make_parser_table()
 $ foreach {parser_data/action_table/row}
-                       action_table_row${position()}[-sep ','-]
+static int parser_action_row${position()}[] = {
+       ${count(action)},
+$   foreach {action}
+       @from, @action, @to[-sep ','-]
+$   end foreach
+};
 $ end foreach
-               ]
-       end
 
+const int* const parser_action_table[] = {
 $ foreach {parser_data/action_table/row}
-       private meth action_table_row${position()}: Array[Int]
-       do
-               return [
-$   foreach {action}
-                               @from, @action, @to [-sep ','-]
-$   end foreach
-                       ]
-       end
+       parser_action_row${position()}[-sep ','-]
 $ end foreach
+};
 
-       attr _goto_table: Array[Array[Int]]
-       private meth build_goto_table
-       do
-               _goto_table = once [ 
 $ foreach {parser_data/goto_table/row}
-                       [
+static int parser_goto_row${position()}[] = {
+       ${count(goto)},
 $   foreach {goto}
-                               @from, @to [-sep ','-]
+       @from, @to[-sep ','-]
 $   end foreach
-                       ] [-sep ','-]
+};
 $ end foreach
-               ]
-       end
 
-       private meth error_messages: Array[String]
-       do
-               return once [
-$ foreach {parser_data/error_messages/msg}
-                       "${sablecc:string2escaped_unicode(.)}" [-sep ','-]
-$ end
-               ]
-       end
-
-       private meth errors: Array[Int]
-       do
-               return once [
-                       [-foreach {parser_data/errors/i}-]${.} [-sep ','-] [-end-]
-               ]
-       end
-end
+const int* const parser_goto_table[] = {
+$ foreach {parser_data/goto_table/row}
+       parser_goto_row${position()}[-sep ','-]
+$ end foreach
+};
 $ end template