Grammalecte  Diff

Differences From Artifact [22d5e1895e]:

To Artifact [9d8b7e574a]:


69
70
71
72
73
74
75
76

77
78
79
80
81
82
83
69
70
71
72
73
74
75

76
77
78
79
80
81
82
83







-
+







On the second pass, you can write regex rules and token rules. All tokens rules must be written within a graph.


## REGEX RULE SYNTAX ##

    __LCR/option(rulename)!priority__
        pattern
            <<- condition ->> error_suggestions             # message_error|URL
            <<- condition ->> error_suggestions         && message_error|URL
            <<- condition ~>> text_rewriting
            <<- condition =>> commands_for_disambiguation
            ...

Patterns are written with the Python syntax for regular expressions:
http://docs.python.org/library/re.html

128
129
130
131
132
133
134
135

136
137
138
139
140
141
142
143
144
145
146

147
148
149
150
151
152


153
154
155
156
157
158
159
160
161

162
163
164
165
166
167
168
169
170
171
172
173

174
175
176
177

178
179
180
181

182
183
184
185
186
187
188
128
129
130
131
132
133
134

135
136
137
138
139
140
141
142
143
144
145

146
147
148
149
150


151
152
153
154
155
156
157
158
159
160

161
162
163
164
165
166
167
168
169
170
171
172

173
174
175
176

177
178
179
180

181
182
183
184
185
186
187
188







-
+










-
+




-
-
+
+








-
+











-
+



-
+



-
+







Each rule name must be unique.

Example. Recognize and suggest missing hyphen and rewrite internally the text
with the hyphen:

    __[s](rulename)__
        foo bar
            <<- ->> foo-bar     # Missing hyphen.
            <<- ->> foo-bar     && Missing hyphen.
            <<- ~>> foo-bar


### Simple-line or multi-line rules

Rules can be break to multiple lines by leading spaces.
You should use 4 spaces.

Examples:

    __<s>(rulename)__ pattern <<- condition ->> replacement # message
    __<s>(rulename)__ pattern <<- condition ->> replacement  && message

    __<s>(rulename)__
        pattern
            <<- condition ->> replacement
            # message
            <<- condition ->> suggestion # message
            && message
            <<- condition ->> suggestion  && message
            <<- condition ~>> text_rewriting
            <<- =>> disambiguation


### Whitespaces at the border of patterns or suggestions

Example: Recognize double or more spaces and suggests a single space:

    __<s>(rulename)__  "  +" <<- ->> " "      # Remove extra space(s).
    __<s>(rulename)__  "  +" <<- ->> " "      && Remove extra space(s).

Characters `"` protect spaces in the pattern and in the replacement text.


### Pattern groups and back references

It is usually useful to retrieve parts of the matched pattern. We simply use
parenthesis in pattern to get groups with back references.

Example. Suggest a word with correct quotation marks:

    \"(\w+)\" <<- ->> “\1”      # Correct quotation marks.
    \"(\w+)\" <<- ->> “\1”      && Correct quotation marks.

Example. Suggest the missing space after the signs `!`, `?` or `.`:

    \b([?!.])([A-Z]+) <<- ->> \1 \2     # Missing space?
    \b([?!.])([A-Z]+) <<- ->> \1 \2     && Missing space?

Example. Back reference in messages.

    (fooo) bar <<- ->> foo      # “\1” should be:
    (fooo) bar <<- ->> foo      && “\1” should be:


### Group positioning codes for JavaScript:

There is no way in JavaScript to know where a captured group starts and ends. To avoid misplacement, regex rules may specify group positioning codes which indicate to the grammar checker where is the position of the captured groups.

A group positioning code always begins by `@@`. If there is several codes, they are separated by a comma `,`.
202
203
204
205
206
207
208
209

210
211
212
213

214
215
216
217
218
219
220
202
203
204
205
206
207
208

209
210
211
212

213
214
215
216
217
218
219
220







-
+



-
+







    " ([?!;])"  @@1


### Pattern matching

Repeating pattern matching of a single rule continues after the previous matching, so instead of general multiword patterns, like

    (\w+) (\w+) <<- some_check(\1, \2) ->> \1, \2 # foo
    (\w+) (\w+) <<- some_check(\1, \2) ->> \1, \2 && foo

use

    (\w+) <<- some_check(\1, word(1)) ->> \1, # foo
    (\w+) <<- some_check(\1, word(1)) ->> \1, && foo


## TOKEN RULES ##

Token rules must be defined within a graph.

### Token rules syntax
289
290
291
292
293
294
295
296

297
298
299
300
301
302
303
304
305

306
307
308
309
310
311
312
289
290
291
292
293
294
295

296
297
298
299
300
301
302
303
304

305
306
307
308
309
310
311
312







-
+








-
+







evaluated as boolean. You can use the usual Python syntax and libraries.

With regex rules, you can call pattern subgroups via `\1`, `\2`… `\0` is the full pattern.

Example:

    these (\w+)
        <<- \1 == "man" -1>> men        # Man is a singular noun.
        <<- \1 == "man" -1>> men        && Man is a singular noun.

You can also apply functions to subgroups like: `\1.startswith("a")` or `\3.islower()` or `re.search("pattern", \2)`.

With token rules, you can also call each token with their reference, like `\1`, `\2`... or `\-1`, `\-2`...

Example:

    foo [really|often|sometimes] bar
        <<- ->> \1 \-1                  # We say “foo bar”.
        <<- ->> \1 \-1                  && We say “foo bar”.


### Functions for regex rules

`word(n)`

>   Catches the nth next word after the pattern (separated only by white spaces).
391
392
393
394
395
396
397
398

399
400
401
402
403
404
405
391
392
393
394
395
396
397

398
399
400
401
402
403
404
405







-
+








### Default variables

`sCountry`

>   Contains the current country locale of the checked paragraph.

    colour <<- sCountry == "US" ->> color   # Use American English spelling.
    colour <<- sCountry == "US" ->> color   && Use American English spelling.

`sContext`

>   The name of the application running (Python, Writer…)


## ACTIONS ##
426
427
428
429
430
431
432
433

434
435
436
437
438
439
440
441
442

443
444
445
446

447
448
449
450
451
452
453
426
427
428
429
430
431
432

433
434
435
436
437
438
439
440
441

442
443
444
445

446
447
448
449
450
451
452
453







-
+








-
+



-
+







effect of rules by specifying a back reference group of the pattern or token references.

Instead of writing `->>`, write `-n>>`  n being the number of a back reference
group. Actually,  `->>`  is similar to  `-0>>`.

Example:

    (ying) and yang <<- -1>> yin # Did you mean:
    (ying) and yang <<- -1>> yin   && Did you mean:

    __[s]__ (Mr.) [A-Z]\w+ <<- ~1>> Mr


**Comparison**

Rule A:

    ying and yang       <<- ->>     yin and yang        # Did you mean:
    ying and yang       <<- ->>     yin and yang        && Did you mean:

Rule B:

    (ying) and yang     <<- -1>>    yin                 # Did you mean:
    (ying) and yang     <<- -1>>    yin                 && Did you mean:

With the rule A, the full pattern is underlined:

    ying and yang
    ^^^^^^^^^^^^^

With the rule B, only the first group is underlined:
462
463
464
465
466
467
468
469

470
471
472
473
474
475
476
477
478

479
480
481
482
483
484
485
486

487
488
489
490
491
492
493
494

495
496
497
498
499
500
501
462
463
464
465
466
467
468

469
470
471
472
473
474
475
476
477

478
479
480
481
482
483
484
485

486
487
488
489
490
491
492
493

494
495
496
497
498
499
500
501







-
+








-
+







-
+







-
+








#### Multiple suggestions

Use `|` in the replacement text to add multiple suggestions:

Example. Foo, FOO, Bar and BAR suggestions for the input word "foo".

    foo <<- ->> Foo|FOO|Bar|BAR         # Did you mean:
    foo <<- ->> Foo|FOO|Bar|BAR         && Did you mean:

#### No suggestion

You can display message without making suggestions. For this purpose,
use a single character _ in the suggestion field.

Example. No suggestion.

    foobar <<- ->> _                    # Message
    foobar <<- ->> _                    && Message

#### Longer explanations with URLs

Warning messages can contain optional URL for longer explanations.

    your’s
        <<- ->> yours
            # Possessive pronoun:|http://en.wikipedia.org/wiki/Possessive_pronoun
            && Possessive pronoun:|http://en.wikipedia.org/wiki/Possessive_pronoun

#### Expressions in suggestion or replacement

Suggestions started by an equal sign are Python string expressions extended with possible back references and named definitions:

Example:

    <<- ->> ='"' + \1.upper() + '"'      # With uppercase letters and quotation marks
    <<- ->> ='"' + \1.upper() + '"'      && With uppercase letters and quotation marks
    <<- ~>> =\1.upper()


### Text rewriting

**WARNING**: The replacing text must be shorter than the replaced text or have the same length. Breaking this rule will misplace following error reports.
You have to ensure yourself the rules comply with this constraint, the text processor won’t do it for you.
568
569
570
571
572
573
574
575

576
577
578
579
580
581
582
568
569
570
571
572
573
574

575
576
577
578
579
580
581
582







-
+







    These         cats  are     blacks.
    These         cats are              blacks.

These grammar mistakes can be detected with one simple rule:

    these +(\w+) +are +(\w+s)
        <<- morph(\1, "noun") and morph(\2, "plural")
        -2>> _              # Adjectives are invariable.
        -2>> _              && Adjectives are invariable.

Instead of replacing text with whitespaces, you can replace text with @.

    https?://\S+ <<- ~>> @

This is useful if at first pass you write rules to check successive whitespaces.
@ are automatically removed at the second pass.
669
670
671
672
673
674
675
676

677
678
679

669
670
671
672
673
674
675

676
677
678

679







-
+


-
+

Example:

    DEF: word_3_letters     \w\w\w+  
    DEF: uppercase_token    ~^[A-Z]+$
    DEF: month_token        [January|February|March|April|May|June|July|August|September|October|November|december]

    ({word_3_letters}) (\w+) <<- condition ->> suggestion     # message|URL
    ({word_3_letters}) (\w+) <<- condition ->> suggestion     && message|URL

    {uppercase_token} {month_token}
        <<- condition ->> message                             # message|URL
        <<- condition ->> message                             && message|URL