$ // This file is part of NIT ( http://www.nitlanguage.org ). $ // $ // Copyright 2008 Jean Privat $ // 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() redef class Parser redef fun build_reduce_table do _reduce_table = new Array[ReduceAction].with_items( $ foreach {rules/rule} new ReduceAction@index(@leftside)[-sep ','-] $ end foreach ) end end $ foreach {rules/rule} private class ReduceAction@index super ReduceAction redef fun action(p: Parser) do 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")} 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 nullable @etype $ end $ when {@cmd='ADDNODE'} if ${translate(@node,"ABCDEFGHIJKLMNOPQRSTUVWXYZ","abcdefghijklmnopqrstuvwxyz")} != null then ${translate(@tolist,"ABCDEFGHIJKLMNOPQRSTUVWXYZ","abcdefghijklmnopqrstuvwxyz")}.add(${translate(@node,"ABCDEFGHIJKLMNOPQRSTUVWXYZ","abcdefghijklmnopqrstuvwxyz")}) end $ end $ when {@cmd='ADDLIST'} ${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'} $ if {count(arg)!=0} var ${translate(@result,"ABCDEFGHIJKLMNOPQRSTUVWXYZ","abcdefghijklmnopqrstuvwxyz")}: nullable @etype = new @etype.init_${translate(@etype,"ABCDEFGHIJKLMNOPQRSTUVWXYZ","abcdefghijklmnopqrstuvwxyz")}( $ foreach {arg} $ if @null null[-sep ','-] $ else ${translate(.,"ABCDEFGHIJKLMNOPQRSTUVWXYZ","abcdefghijklmnopqrstuvwxyz")}[-sep ','-] $ 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 node_list = null $ else node_list = ${translate(@node,"ABCDEFGHIJKLMNOPQRSTUVWXYZ","abcdefghijklmnopqrstuvwxyz")} $ end $ end $ when {@cmd='RETURNLIST'} node_list = ${translate(@list,"ABCDEFGHIJKLMNOPQRSTUVWXYZ","abcdefghijklmnopqrstuvwxyz")} $ end $ end choose $ end foreach p.push(p.go_to(_goto), node_list) end end $ end foreach $ end template $ template make_parser_table() $ foreach {parser_data/action_table/row} static int parser_action_row${position()}[] = { ${count(action)}, $ foreach {action} @from, @action, @to[-sep ','-] $ end foreach }; $ end foreach const int* const parser_action_table[] = { $ foreach {parser_data/action_table/row} parser_action_row${position()}[-sep ','-] $ end foreach }; $ foreach {parser_data/goto_table/row} static int parser_goto_row${position()}[] = { ${count(goto)}, $ foreach {goto} @from, @to[-sep ','-] $ end foreach }; $ end foreach const int* const parser_goto_table[] = { $ foreach {parser_data/goto_table/row} parser_goto_row${position()}[-sep ','-] $ end foreach }; $ end template