1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131
|
Creator-Defined Statements
==========================
Creator-defined statements allow you to add your own statements to Ren'Py. This
makes it possible to add things that are not supported by the current syntax of
Ren'Py.
Creator-defined statements must be defined in a ``python early`` block. What's more,
the filename containing the user-defined statement must be be loaded earlier
than any file that uses it. Since Ren'Py loads files in Unicode sort order, it
generally makes sense to prefix the name of any file containing a user-defined
statement with 01, or some other small number.
A user-defined statement cannot be used in the file in which it is defined.
Creator-defined statement are registered using the :func:`renpy.register_statement`
function.
.. include:: inc/statement_register
The parse method takes a Lexer object:
.. class:: Lexer
.. method:: eol()
True if the lexer is at the end of the line.
.. method:: match(re)
Matches an arbitrary regexp string.
All of the statements in the lexer that match things are implemented
in terms of this function. They first skip whitespace, then attempt
to match against the line. If the match succeeds, the matched text
is returned. Otherwise, None is returned.
.. method:: keyword(s)
Matches `s` as a keyword.
.. method:: name()
Matches a name. This does not match built-in keywords.
.. method:: word()
Matches any word, including keywords. Returns the text of the
matched word.
.. method:: string()
Matches a Ren'Py string.
.. method:: integer()
Matches an integer, returns a string containing the integer.
.. method:: float()
Matches a floating point number, returns a string containing the
floating point number.
.. method:: simple_expression()
Matches a simple Python expression, returns it as a string.
.. method:: rest()
Skips whitespace, then returns the rest of the line.
.. method:: checkpoint()
Returns an opaque object representing the current state of the lexer.
.. method:: revert(o)
When `o` is the object returned from checkpoint(), reverts the state
of the lexer to what it was when checkpoint() was called. (This is
used for backtracking.)
.. method:: subblock_lexer()
Return a Lexer for the block associated with the current line.
.. method:: advance()
In a subblock lexer, advances to the next line. This must be called
before the first line, so the first line can be parsed.
Lint Utility Functions
----------------------
These functions are useful in writing lint functions.
.. include:: inc/lint
Example
-------
This creates a new statement ``line`` that allows lines of text to be specified
without quotes. ::
python early:
def parse_smartline(lex):
who = lex.simple_expression()
what = lex.rest()
return (who, what)
def execute_smartline(o):
who, what = o
renpy.say(eval(who), what)
def lint_smartline(o):
who, what = o
try:
eval(who)
except:
renpy.error("Character not defined: %s" % who)
tte = renpy.check_text_tags(what)
if tte:
renpy.error(tte)
renpy.register_statement("line", parse=parse_smartline, execute=execute_smartline, lint=lint_smartline)
This can be used by writing::
line e "These quotes will show up," Eileen said, "and don't need to be backslashed."
|