#-----------------------------------------------------------------------------* # # @file gtl_parser.galgas # # @section desc File description # # Parsing of GTL. # # @section copyright Copyright # # Goil OIL compiler, part of Trampoline RTOS # # Trampoline is copyright (c) CNRS, University of Nantes, # Ecole Centrale de Nantes # Trampoline is protected by the French intellectual property law. # # This software is distributed under the GNU Public Licence V2. # Check the LICENSE file in the root directory of Trampoline # # $Date$ # $Rev$ # $Author$ # $URL$ # #-----------------------------------------------------------------------------* syntax gtl_parser (gtl_scanner) { rule !@gtlExpression expression rule !@gtlVarPath path rule !@gtlVarPath variable !@bool hereInstead rule ?@gtlContext context ?!@library lib rule !@gtlInstruction instruction rule !@gtlArgumentList arguments #-----------------------------------------------------------------------------* rule ? @gtlContext context ?!@library lib ! @gtlInstructionList program { repeat while !context !?lib end ?program } #-----------------------------------------------------------------------------* rule !@gtlInstructionList instructionList { instructionList = .emptyList repeat let @string templateString = @string.retrieveAndResetTemplateString if templateString != "" then instructionList += !@gtlTemplateStringInstruction.new{ !.here !"" !templateString } end while ?let @gtlInstruction instruction instructionList += !instruction while ?let @gtlInstruction instruction instructionList += !instruction end } #-----------------------------------------------------------------------------* # emit parsing. Syntax is: # ! expression #-----------------------------------------------------------------------------* rule !@gtlInstruction instruction { $!$ let @location where = .here ?let @gtlExpression expression instruction = @gtlEmitInstruction.new { !where !signature(!where) !expression } } #-----------------------------------------------------------------------------* # write to parsing. Syntax is: # write to expression : # ... # end write #-----------------------------------------------------------------------------* rule !@gtlInstruction instruction { @bool isExecutable = false $write$ let @location where = .here $to$ select $executable$ isExecutable = true or end ?let @gtlExpression fileName $:$ ?let @gtlInstructionList instructionList $end$ $write$ instruction = @gtlWriteToInstruction.new { !where !signature(!where) !fileName !isExecutable !instructionList } } #-----------------------------------------------------------------------------* rule !@gtlInstruction instruction { let @lstring prefix @bool ifExists = false @gtlInstructionList instructionList = .emptyList let @gtlExpression fileName @gtlExpressionList templateArguments = .emptyList @bool globalTemplate = true $template$ let @location where = .here select globalTemplate = false $($ select repeat ?let @gtlExpression expression templateArguments += !expression while $,$ end or end $)$ or end select $if$ $exists$ ifExists = true ?fileName select $in$ $identifier$ ?prefix or prefix = .new { !"" !@location.here } end select $or$ ?instructionList $end$ $template$ or end or ?fileName select $in$ $identifier$ ?prefix or prefix = .new { !"" !@location.here } end end instruction = @gtlTemplateInstruction.new { !where !signature(!where) !prefix !fileName !ifExists !globalTemplate !templateArguments !instructionList } } #-----------------------------------------------------------------------------* rule !@gtlInstruction instruction { $?$ let @location where = .here ?let @gtlVarPath columnConstantName instruction = @gtlGetColumnInstruction.new { !where !signature(!where) !columnConstantName } } #-----------------------------------------------------------------------------* rule !@gtlInstruction instruction { @gtlThenElsifStatementList thenElsifList = .emptyList @gtlInstructionList elseList = .emptyList $if$ let @location where = .here repeat ?let @gtlExpression condition $then$ ?let @gtlInstructionList instructionList thenElsifList += !condition !instructionList while $elsif$ end #--- else select $else$ ?elseList or end #--- $end$ $if$ instruction = @gtlIfStatementInstruction.new { !where !signature(!where) !thenElsifList !elseList } } #-----------------------------------------------------------------------------* rule !@gtlInstruction instruction { @gtlInstructionList beforeList = .emptyList let @gtlInstructionList doList @gtlInstructionList betweenList = .emptyList @gtlInstructionList afterList = .emptyList let @lstring indexName let @lstring variableName $foreach$ let @location where = .here $identifier$ ?var @lstring keyName select $,$ $identifier$ ?variableName or variableName = keyName keyName = .new { !"" !where } end select $($ $identifier$ ?indexName $)$ or indexName = .new { !"INDEX" !.here } end $in$ ?let @gtlExpression iterable #--- before block select $before$ ?beforeList or end #--- do block $do$ ?doList #--- between block select $between$ ?betweenList or end #--- after block select $after$ ?afterList or end #--- $end$ $foreach$ instruction = @gtlForeachStatementInstruction.new { !where !signature(!where) !keyName !variableName !indexName !iterable !beforeList !betweenList !afterList !doList } } #-----------------------------------------------------------------------------* rule !@gtlInstruction instruction { let @gtlInstructionList doList @gtlInstructionList betweenList = .emptyList @gtlExpressionList iterable = .emptyList $for$ let @location where = .here if [option gtl_options.warnDeprecated value] then warning .here : "for var in expression, ..., expression is deprecated" fixit { replace "foreach var in @( expression, ..., expression )" } end $identifier$ ?let @lstring variableName $in$ repeat ?let @gtlExpression expression iterable += !expression while $,$ end #--- Do block $do$ ?doList #--- between block select $between$ ?betweenList or end #--- $end$ $for$ instruction = @gtlForStatementInstruction.new { !where !signature(!where) !variableName !iterable !betweenList !doList } } #-----------------------------------------------------------------------------* rule !@gtlInstruction instruction { @gtlInstructionList beforeList = .emptyList @gtlInstructionList betweenList = .emptyList @gtlInstructionList afterList = .emptyList @sint64 direction = 1 $loop$ let @location where = .here @gtlExpression step = @gtlTerminal.new { !where !@gtlInt.new { !where !emptylstring() !1 } } $identifier$ ?let @lstring variableName $from$ ?let @gtlExpression start select or $up$ or $down$ direction = -1 end $to$ ?let @gtlExpression stop select $step$ ?step or end #--- before block select $before$ ?beforeList or end #--- do block $do$ ?let @gtlInstructionList doList #--- between block select $between$ ?betweenList or end #--- after block select $after$ ?afterList or end #--- $end$ $loop$ instruction = @gtlLoopStatementInstruction.new { !where !signature(!where) !variableName !start !stop !step !direction !beforeList !betweenList !afterList !doList } } #-----------------------------------------------------------------------------* rule !@gtlInstruction instruction { let @gtlExpression limit $repeat$ let @location where = .here select $($ ?limit $)$ or limit = @gtlTerminal.new { !.here !@gtlInt.new{ !.here !emptylstring() ![@uint.max{} bigint] } } end ?let @gtlInstructionList continueList $while$ ?let @gtlExpression condition $do$ ?let @gtlInstructionList doList $end$ $repeat$ instruction = @gtlRepeatStatementInstruction.new { !where !signature(!where) !limit !condition !continueList !doList } } #-----------------------------------------------------------------------------* rule !@gtlInstruction instruction { $input$ let @location where = .here ?let @gtlArgumentList arguments instruction = @gtlInputStatementInstruction.new { !where !signature(!where) !arguments } } #-----------------------------------------------------------------------------* rule !@gtlExpression expression { select $identifier$ ?let @lstring fileName expression = @gtlTerminal.new { !@location.here !@gtlString.new { ![fileName location] !emptylstring() ![fileName string] } } or $from$ ?expression end } } # end of syntax