From a77e6be636010646cf5decc5fe103862353486fb Mon Sep 17 00:00:00 2001 From: "Johannes E. Krause" Date: Wed, 13 Mar 2013 21:55:12 +0100 Subject: [PATCH] Feature: allow inserting ActionC comments --- nml/actions/actionC.py | 33 ++++++++++++++++++++++++ nml/ast/comment.py | 38 ++++++++++++++++++++++++++++ nml/parser.py | 7 ++++- nml/tokens.py | 1 + regression/031_comment.nml | 9 +++++++ regression/expected/031_comment.grf | Bin 0 -> 158 bytes regression/expected/031_comment.nfo | 20 +++++++++++++++ 7 files changed, 107 insertions(+), 1 deletion(-) create mode 100644 nml/actions/actionC.py create mode 100644 nml/ast/comment.py create mode 100644 regression/031_comment.nml create mode 100644 regression/expected/031_comment.grf create mode 100644 regression/expected/031_comment.nfo diff --git a/nml/actions/actionC.py b/nml/actions/actionC.py new file mode 100644 index 00000000..773361da --- /dev/null +++ b/nml/actions/actionC.py @@ -0,0 +1,33 @@ +__license__ = """ +NML is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2 of the License, or +(at your option) any later version. + +NML is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU General Public License for more details. + +You should have received a copy of the GNU General Public License along +with NML; if not, write to the Free Software Foundation, Inc., +51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.""" + +from nml.actions import base_action + +class ActionC(base_action.BaseAction): + def __init__(self, text): + self.text = text + + def write(self, file): + # * 0C [] + size = len(self.text)+1 + file.start_sprite(size) + file.print_bytex(0x0C) + file.print_string(self.text, final_zero = False) + file.newline() + file.end_sprite() + +def parse_actionC(comment): + return [ActionC(comment.text.value)] + diff --git a/nml/ast/comment.py b/nml/ast/comment.py new file mode 100644 index 00000000..339bce74 --- /dev/null +++ b/nml/ast/comment.py @@ -0,0 +1,38 @@ +__license__ = """ +NML is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2 of the License, or +(at your option) any later version. + +NML is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU General Public License for more details. + +You should have received a copy of the GNU General Public License along +with NML; if not, write to the Free Software Foundation, Inc., +51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.""" + +from nml.actions import actionC +from nml.ast import base_statement +from nml import expression, generic + +class Comment(base_statement.BaseStatement): + def __init__(self, text, pos): + base_statement.BaseStatement.__init__(self, "comment()", pos) + self.text = text + + def pre_process(self): + self.text = self.text.reduce() + if not isinstance(self.text, expression.StringLiteral): + raise generic.ScriptError("Comment must be a string literal.", self.text.pos) + + def debug_print(self, indentation): + generic.print_dbg(indentation, 'Comment:') + self.text.debug_print(indentation + 2) + + def get_action_list(self): + return actionC.parse_actionC(self) + + def __str__(self): + return 'comment(%s);\n' % (self.text,) diff --git a/nml/parser.py b/nml/parser.py index 7542af97..abae67c2 100644 --- a/nml/parser.py +++ b/nml/parser.py @@ -14,7 +14,7 @@ 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.""" from nml import generic, expression, tokens, nmlop, unit -from nml.ast import assignment, basecost, cargotable, conditional, deactivate, disable_item, error, font, general, grf, item, loop, produce, tracktypetable, replace, spriteblock, switch, townnames, snowline, skipall, tilelayout, alt_sprites, base_graphics, override, sort_vehicles +from nml.ast import assignment, basecost, cargotable, conditional, deactivate, disable_item, error, font, general, grf, item, loop, produce, tracktypetable, replace, spriteblock, switch, townnames, snowline, skipall, tilelayout, alt_sprites, base_graphics, override, sort_vehicles, comment from nml.actions import actionD, real_sprite import ply.yacc as yacc @@ -119,6 +119,7 @@ def p_main_block(self, t): | snowline | engine_override | sort_vehicles + | comment | basecost''' t[0] = t[1] @@ -719,6 +720,10 @@ def p_sort_vehicles(self, t): 'sort_vehicles : SORT_VEHICLES LPAREN expression_list RPAREN SEMICOLON' t[0] = sort_vehicles.SortVehicles(t[3], t.lineno(1)) + def p_comment(self, t): + 'comment : COMMENT LPAREN expression RPAREN SEMICOLON' + t[0] = comment.Comment(t[3], t.lineno(1)) + def p_tilelayout(self, t): 'tilelayout : TILELAYOUT ID LBRACE tilelayout_list RBRACE' t[0] = tilelayout.TileLayout(t[2], t[4], t.lineno(1)) diff --git a/nml/tokens.py b/nml/tokens.py index 616f3e23..716696fc 100644 --- a/nml/tokens.py +++ b/nml/tokens.py @@ -57,6 +57,7 @@ 'recolour_sprite' : 'RECOLOUR_SPRITE', 'engine_override' : 'ENGINE_OVERRIDE', 'sort' : 'SORT_VEHICLES', + 'comment' : 'COMMENT', } line_directive1_pat = re.compile(r'\#line\s+(\d+)\s*(\r?\n|"(.*)"\r?\n)') diff --git a/regression/031_comment.nml b/regression/031_comment.nml new file mode 100644 index 00000000..649bcb85 --- /dev/null +++ b/regression/031_comment.nml @@ -0,0 +1,9 @@ +grf { + grfid: "NML\31"; + name: string(STR_REGRESSION_NAME); + desc: string(STR_REGRESSION_DESC); + version: 0; + min_compatible_version: 0; +} + +comment("test"); \ No newline at end of file diff --git a/regression/expected/031_comment.grf b/regression/expected/031_comment.grf new file mode 100644 index 0000000000000000000000000000000000000000..d6966aac4bb008debf0315015cc6fa7a02246ee4 GIT binary patch literal 158 zcmZQza1U~8;^mU!>R|u^79jr53?$4z3=wBfKR17;u%KW+kOTvRldq>=7?kN3;26XR z6bf+k31(z)baL_u0rD*v7)(G~IXL`$eGGwEp(r)ID7CmaGe1wEB(=DN!4XU; 2u< 2u> 2/ 2% 2u/ 2u% 2* 2& 2| 2^ 2sto = 2s 2rst = 2r 2psto 2ror = 2rot 2cmp 2ucmp 2<< 2u>> 2>> +// Escapes: 71 70 7= 7! 7< 7> 7G 7g 7gG 7GG 7gg 7c 7C +// Escapes: D= = DR D+ = DF D- = DC Du* = DM D* = DnF Du<< = DnC D<< = DO D& D| Du/ D/ Du% D% +// Format: spritenum imagefile depth xpos ypos xsize ysize xrel yrel zoom flags + +0 * 4 \d3 + +1 * 54 14 "C" "INFO" +"B" "VRSN" \w4 \dx00000000 +"B" "MINV" \w4 \dx00000000 +"B" "NPAR" \w1 00 +"B" "PALS" \w1 "A" +"B" "BLTR" \w1 "8" +00 +00 +2 * 52 08 08 "NML\31" "NML regression test" 00 "A test newgrf testing NML" 00 +3 * 5 0C "test" +