pegs/re =~ warns about scope rule change; improved docs

This commit is contained in:
Araq 2013-05-08 15:49:04 +02:00
commit 44c4b945eb
4 changed files with 19 additions and 9 deletions

View file

@ -735,12 +735,10 @@ However, this cannot be done for mutually recursive procedures:
proc even(n: int): bool proc even(n: int): bool
proc odd(n: int): bool = proc odd(n: int): bool =
if n == 1: return true n == 1 or even(n-1)
else: return even(n-1)
proc even(n: int): bool = proc even(n: int): bool =
if n == 0: return true n == 0 or odd(n-1)
else: return odd(n-1)
Here ``odd`` depends on ``even`` and vice versa. Thus ``even`` needs to be Here ``odd`` depends on ``even`` and vice versa. Thus ``even`` needs to be
introduced to the compiler before it is completely defined. The syntax for introduced to the compiler before it is completely defined. The syntax for
@ -750,6 +748,9 @@ procedure's body.
Later versions of the language may get rid of the need for forward Later versions of the language may get rid of the need for forward
declarations. declarations.
The example also shows that a proc's body can consist of a single expression
whose value is then returned implicitly.
Iterators Iterators
========= =========
@ -848,7 +849,7 @@ procedure; the length never counts the terminating zero. Accessing the
terminating zero is no error and often leads to simpler code: terminating zero is no error and often leads to simpler code:
.. code-block:: nimrod .. code-block:: nimrod
if s[i] == 'a' and s[i+1] == 'b' and s[i+2] == '\0': if s[i] == 'a' and s[i+1] == 'b':
# no need to check whether ``i < len(s)``! # no need to check whether ``i < len(s)``!
... ...
@ -1168,7 +1169,7 @@ subdivided in height levels accessed through their integer index:
echo len(tower) # --> 10 echo len(tower) # --> 10
echo len(tower[1]) # --> 4 echo len(tower[1]) # --> 4
echo repr(tower) # --> [[slowBlink, mediumBlink, ...more output.. echo repr(tower) # --> [[slowBlink, mediumBlink, ...more output..
# The following lines don't compile due to type mistmatch errors # The following lines don't compile due to type mismatch errors
#tower[north][east] = on #tower[north][east] = on
#tower[0][1] = on #tower[0][1] = on

View file

@ -242,6 +242,7 @@ template `=~` *(s: string, pattern: TRegEx): expr =
bind maxSubPatterns bind maxSubPatterns
when not definedInScope(matches): when not definedInScope(matches):
var matches {.inject.}: array[0..maxSubPatterns-1, string] var matches {.inject.}: array[0..maxSubPatterns-1, string]
{.warning: "injected 'matches' might be affected by new scoping rules in 0.9.4".}
match(s, pattern, matches) match(s, pattern, matches)
# ------------------------- more string handling ------------------------------ # ------------------------- more string handling ------------------------------

View file

@ -870,6 +870,7 @@ template `=~`*(s: string, pattern: TPeg): bool =
bind maxSubpatterns bind maxSubpatterns
when not definedInScope(matches): when not definedInScope(matches):
var matches {.inject.}: array[0..maxSubpatterns-1, string] var matches {.inject.}: array[0..maxSubpatterns-1, string]
{.warning: "injected 'matches' might be affected by new scoping rules in 0.9.4".}
match(s, pattern, matches) match(s, pattern, matches)
# ------------------------- more string handling ------------------------------ # ------------------------- more string handling ------------------------------
@ -1718,6 +1719,7 @@ when isMainModule:
assert match("_______ana", peg"A <- 'ana' / . A") assert match("_______ana", peg"A <- 'ana' / . A")
assert match("abcs%%%", peg"A <- ..A / .A / '%'") assert match("abcs%%%", peg"A <- ..A / .A / '%'")
var matches: array[0..maxSubpatterns-1, string]
if "abc" =~ peg"{'a'}'bc' 'xyz' / {\ident}": if "abc" =~ peg"{'a'}'bc' 'xyz' / {\ident}":
assert matches[0] == "abc" assert matches[0] == "abc"
else: else:
@ -1742,8 +1744,6 @@ when isMainModule:
else: else:
assert false assert false
when not definedInScope(matches):
var matches: array[0..maxSubpatterns-1, string]
if match("abcdefg", peg"c {d} ef {g}", matches, 2): if match("abcdefg", peg"c {d} ef {g}", matches, 2):
assert matches[0] == "d" assert matches[0] == "d"
assert matches[1] == "g" assert matches[1] == "g"

View file

@ -30,7 +30,7 @@ Library Additions
- ``system.fields`` and ``system.fieldPairs`` support ``object`` too; they - ``system.fields`` and ``system.fieldPairs`` support ``object`` too; they
used to only support tuples. used to only support tuples.
- Added ``system.CurrentSourcePath`` returning the full file-system path of - Added ``system.CurrentSourcePath`` returning the full file-system path of
the current source file the current source file.
Changes affecting backwards compatibility Changes affecting backwards compatibility
@ -39,6 +39,12 @@ Changes affecting backwards compatibility
- ``shared`` is a keyword now. - ``shared`` is a keyword now.
- Deprecated ``sockets.recvLine`` and ``asyncio.recvLine``, added - Deprecated ``sockets.recvLine`` and ``asyncio.recvLine``, added
``readLine`` instead. ``readLine`` instead.
- The way indentation is handled in the parser changed significantly. However,
this affects very little (if any) real world code.
- The expression/statement unification has been implemented. Again this
only affects edge cases and no known real world code.
- The scope rules of ``if`` statements will change in 0.9.4. This affects the
``=~`` pegs/re templates.
Compiler Additions Compiler Additions
@ -74,6 +80,8 @@ Language Additions
- Overloading based on ASTs has been implemented. - Overloading based on ASTs has been implemented.
- Generics are now supported for multi methods. - Generics are now supported for multi methods.
- Objects can be initialized via an *object constructor expression*. - Objects can be initialized via an *object constructor expression*.
- There is a new syntactic construct ``(;)`` unifying expressions and
statements.
2012-09-23 Version 0.9.0 released 2012-09-23 Version 0.9.0 released