Skip to content

Commit

Permalink
Feature: allow inserting ActionC comments
Browse files Browse the repository at this point in the history
  • Loading branch information
Eddi-z committed Oct 7, 2019
1 parent bcfff83 commit 7c0c076
Show file tree
Hide file tree
Showing 6 changed files with 107 additions and 1 deletion.
33 changes: 33 additions & 0 deletions nml/actions/actionC.py
Original file line number Diff line number Diff line change
@@ -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):
#<Sprite-number> * <Length> 0C [<ignored>]
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)]

38 changes: 38 additions & 0 deletions nml/ast/comment.py
Original file line number Diff line number Diff line change
@@ -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,)
7 changes: 6 additions & 1 deletion nml/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -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, railtypetable, 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, railtypetable, 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

Expand Down Expand Up @@ -108,6 +108,7 @@ def p_main_block(self, t):
| snowline
| engine_override
| sort_vehicles
| comment
| basecost'''
t[0] = t[1]

Expand Down Expand Up @@ -697,6 +698,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))
Expand Down
1 change: 1 addition & 0 deletions nml/tokens.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,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)')
Expand Down
9 changes: 9 additions & 0 deletions regression/031_comment.nml
Original file line number Diff line number Diff line change
@@ -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");
20 changes: 20 additions & 0 deletions regression/expected/031_comment.nfo
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
// Automatically generated by GRFCODEC. Do not modify!
// (Info version 32)
// Escapes: 2+ 2- 2< 2> 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"

0 comments on commit 7c0c076

Please sign in to comment.