Skip to content

Commit a77e6be

Browse files
committed
Feature: allow inserting ActionC comments
1 parent 74cc45f commit a77e6be

7 files changed

Lines changed: 107 additions & 1 deletion

File tree

nml/actions/actionC.py

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
__license__ = """
2+
NML is free software; you can redistribute it and/or modify
3+
it under the terms of the GNU General Public License as published by
4+
the Free Software Foundation; either version 2 of the License, or
5+
(at your option) any later version.
6+
7+
NML is distributed in the hope that it will be useful,
8+
but WITHOUT ANY WARRANTY; without even the implied warranty of
9+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10+
GNU General Public License for more details.
11+
12+
You should have received a copy of the GNU General Public License along
13+
with NML; if not, write to the Free Software Foundation, Inc.,
14+
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA."""
15+
16+
from nml.actions import base_action
17+
18+
class ActionC(base_action.BaseAction):
19+
def __init__(self, text):
20+
self.text = text
21+
22+
def write(self, file):
23+
#<Sprite-number> * <Length> 0C [<ignored>]
24+
size = len(self.text)+1
25+
file.start_sprite(size)
26+
file.print_bytex(0x0C)
27+
file.print_string(self.text, final_zero = False)
28+
file.newline()
29+
file.end_sprite()
30+
31+
def parse_actionC(comment):
32+
return [ActionC(comment.text.value)]
33+

nml/ast/comment.py

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
__license__ = """
2+
NML is free software; you can redistribute it and/or modify
3+
it under the terms of the GNU General Public License as published by
4+
the Free Software Foundation; either version 2 of the License, or
5+
(at your option) any later version.
6+
7+
NML is distributed in the hope that it will be useful,
8+
but WITHOUT ANY WARRANTY; without even the implied warranty of
9+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10+
GNU General Public License for more details.
11+
12+
You should have received a copy of the GNU General Public License along
13+
with NML; if not, write to the Free Software Foundation, Inc.,
14+
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA."""
15+
16+
from nml.actions import actionC
17+
from nml.ast import base_statement
18+
from nml import expression, generic
19+
20+
class Comment(base_statement.BaseStatement):
21+
def __init__(self, text, pos):
22+
base_statement.BaseStatement.__init__(self, "comment()", pos)
23+
self.text = text
24+
25+
def pre_process(self):
26+
self.text = self.text.reduce()
27+
if not isinstance(self.text, expression.StringLiteral):
28+
raise generic.ScriptError("Comment must be a string literal.", self.text.pos)
29+
30+
def debug_print(self, indentation):
31+
generic.print_dbg(indentation, 'Comment:')
32+
self.text.debug_print(indentation + 2)
33+
34+
def get_action_list(self):
35+
return actionC.parse_actionC(self)
36+
37+
def __str__(self):
38+
return 'comment(%s);\n' % (self.text,)

nml/parser.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA."""
1515

1616
from nml import generic, expression, tokens, nmlop, unit
17-
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
17+
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
1818
from nml.actions import actionD, real_sprite
1919
import ply.yacc as yacc
2020

@@ -119,6 +119,7 @@ def p_main_block(self, t):
119119
| snowline
120120
| engine_override
121121
| sort_vehicles
122+
| comment
122123
| basecost'''
123124
t[0] = t[1]
124125

@@ -719,6 +720,10 @@ def p_sort_vehicles(self, t):
719720
'sort_vehicles : SORT_VEHICLES LPAREN expression_list RPAREN SEMICOLON'
720721
t[0] = sort_vehicles.SortVehicles(t[3], t.lineno(1))
721722

723+
def p_comment(self, t):
724+
'comment : COMMENT LPAREN expression RPAREN SEMICOLON'
725+
t[0] = comment.Comment(t[3], t.lineno(1))
726+
722727
def p_tilelayout(self, t):
723728
'tilelayout : TILELAYOUT ID LBRACE tilelayout_list RBRACE'
724729
t[0] = tilelayout.TileLayout(t[2], t[4], t.lineno(1))

nml/tokens.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@
5757
'recolour_sprite' : 'RECOLOUR_SPRITE',
5858
'engine_override' : 'ENGINE_OVERRIDE',
5959
'sort' : 'SORT_VEHICLES',
60+
'comment' : 'COMMENT',
6061
}
6162

6263
line_directive1_pat = re.compile(r'\#line\s+(\d+)\s*(\r?\n|"(.*)"\r?\n)')

regression/031_comment.nml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
grf {
2+
grfid: "NML\31";
3+
name: string(STR_REGRESSION_NAME);
4+
desc: string(STR_REGRESSION_DESC);
5+
version: 0;
6+
min_compatible_version: 0;
7+
}
8+
9+
comment("test");
158 Bytes
Binary file not shown.
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
// Automatically generated by GRFCODEC. Do not modify!
2+
// (Info version 32)
3+
// 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>>
4+
// Escapes: 71 70 7= 7! 7< 7> 7G 7g 7gG 7GG 7gg 7c 7C
5+
// Escapes: D= = DR D+ = DF D- = DC Du* = DM D* = DnF Du<< = DnC D<< = DO D& D| Du/ D/ Du% D%
6+
// Format: spritenum imagefile depth xpos ypos xsize ysize xrel yrel zoom flags
7+
8+
0 * 4 \d3
9+
10+
1 * 54 14 "C" "INFO"
11+
"B" "VRSN" \w4 \dx00000000
12+
"B" "MINV" \w4 \dx00000000
13+
"B" "NPAR" \w1 00
14+
"B" "PALS" \w1 "A"
15+
"B" "BLTR" \w1 "8"
16+
00
17+
00
18+
2 * 52 08 08 "NML\31" "NML regression test" 00 "A test newgrf testing NML" 00
19+
3 * 5 0C "test"
20+

0 commit comments

Comments
 (0)