mediawiki-extensions-AbuseF.../includes/Parser
Sorawee Porncharoenwase 320e3d696f Add a static analyzer for the filter language
This commit adds a class AFPSyntaxChecker which can statically analyze
a filter code to detect the following errors:

- unbound variables (which comes in two modes: conservative and liberal,
  default to conservative)
- unused variables (disabled by default for compatibilty)
- assignment on built-in identifiers
- function application's arity mismatch
- function application's invalid function name
- non-string literal in the first argument of set / set_var

The existing parser and evaluator are modified as follows:

- The new (caching) evaluator no longer needs to perform variable
  hoisting at runtime.
  - Note that for array assignment, this changes the semantics.
- The new parser is more lenient, reducing parsing errors.
  The static analyzer will catch these errors instead, allowing us
  to give a much better error message and reduces the complexity of
  the parser.
  * The parser now allows function name to be any identifier.
  * The parser now allows arity mismatch to occur.
  * The parser now allows the first argument of set to be any expression.

Concretely, obvious changes that users will see are:

1. a := [1]; false & (a[] := 2); a[0] === 1

   would evaluate to true, while it used to evaluate to the undefined value
   due to hoisting

2. f(1)

   will now error with 'f is not a valid function' as opposed to
   'Unexpected "T_BRACE"'

3. length

   will now error with 'Illegal use of built-in identifier "length"'
   as opposed to 'Expected a ('

Appendix: conservative and liberal mode

The conservative mode is completely compatible with the current evaluator.
That is,

false & (a := 1); a

will not deem `a` as unbound, though this is actually undesirable because
`a` would then be bound to the troublesome undefined value.

The liberal mode rejects the above pattern by deeming `a` as unbound.
However, it also rejects

true & (a := 1); a

even though (a := 1) is always executed. Since there are several filters
in Wikimedia projects that rely on this behavior, we default the mode
to conservative for now.

Note that even the liberal mode doesn't really respect lexical scope
appeared in some other programming languages (see also T234690).
For instance:

(if true then (a := 1) else (a := 2) end); a

would be accepted by the liberal checker, even though under lexical scope,
`a` would be unbound. However, it is unlikely that lexical scope
will be suitable for the filter language, as most filters in
Wikimedia projects that have user-defined variable do violate lexical scope.

Bug: T260903
Bug: T238709
Bug: T237610
Bug: T234690
Bug: T231536
Change-Id: Ic6d030503e554933f8d220c6f87b680505918ae2
2021-08-31 03:28:24 +02:00
..
Exception Add a static analyzer for the filter language 2021-08-31 03:28:24 +02:00
AbuseFilterCachingParser.php Add a static analyzer for the filter language 2021-08-31 03:28:24 +02:00
AbuseFilterTokenizer.php Move parser exceptions to specific namespace and rename them 2021-08-29 23:38:31 +00:00
AFPData.php Move parser exceptions to specific namespace and rename them 2021-08-29 23:38:31 +00:00
AFPParserState.php Move parser classes to a dedicated namespace 2020-12-09 01:30:20 +00:00
AFPSyntaxTree.php Add a static analyzer for the filter language 2021-08-31 03:28:24 +02:00
AFPToken.php Move parser classes to a dedicated namespace 2020-12-09 01:30:20 +00:00
AFPTransitionBase.php Add a static analyzer for the filter language 2021-08-31 03:28:24 +02:00
AFPTreeNode.php Add a static analyzer for the filter language 2021-08-31 03:28:24 +02:00
AFPTreeParser.php Add a static analyzer for the filter language 2021-08-31 03:28:24 +02:00
ParserFactory.php build: Updating dependencies 2021-07-21 18:51:18 +00:00
ParserStatus.php Move parser exceptions to specific namespace and rename them 2021-08-29 23:38:31 +00:00
SyntaxChecker.php Add a static analyzer for the filter language 2021-08-31 03:28:24 +02:00