This is an automated email from the ASF dual-hosted git repository.

jimjag pushed a commit to branch odf-devel
in repository https://gitbox.apache.org/repos/asf/openoffice.git

commit de58a9ba5f0eee8e133dd18806a46d0b8f333fe3
Author: Jim Jagielski <[email protected]>
AuthorDate: Mon Oct 26 12:08:31 2026 -0400

    test/odf-validate: check formula and condition syntax
---
 test/odf-validate/README.md                 |  15 +
 test/odf-validate/formula-cases.tsv         | 172 ++++++++
 test/odf-validate/formulacheck.py           | 585 ++++++++++++++++++++++++++++
 test/odf-validate/openformula-functions.txt | 393 +++++++++++++++++++
 test/odf-validate/selftest.py               |  29 +-
 5 files changed, 1193 insertions(+), 1 deletion(-)

diff --git a/test/odf-validate/README.md b/test/odf-validate/README.md
index 8187aa9ee0..570de79694 100644
--- a/test/odf-validate/README.md
+++ b/test/odf-validate/README.md
@@ -61,6 +61,21 @@ The steps:
   `...graphic-template`; the registered `...graphics` and 
`...graphics-template`
   of Part 1 appendix C are accepted. `...text-web` and `...formula-template`
   are registered but no §2.2.x allows them, so they are reported.
+- `formulacheck.py <outdir> <formulas.json>` parses the `style:condition`,
+  `table:condition`, `table:expression`, `table:formula` and `text:formula`
+  values (Part 1 §2.2.1 D.3, §2.2.4 D/E) with the OpenFormula grammar (Part 2
+  chapter 5) and the condition syntaxes of Part 1 §19.468 and §19.596.
+  - Syntax only; no semantics, function arity or type checking.
+  - A prefix bound to another namespace is out of Part 2's reach: `should:` in
+    general, `shall:` for `table:formula` inside `office:spreadsheet` (§2.2.4 
D).
+  - Non-ASCII name characters use Unicode categories instead of the XML 1.0
+    4th edition tables; an IRI is checked by character class, not the full
+    RFC 3987 grammar; `openformula-functions.txt` holds the Part 2 function
+    names.
+  - The condition syntaxes follow the prose of Part 1, which has no grammar:
+    `,` between arguments, lowercase ` and `, `!=` as the relational operator.
+  - `formula-cases.tsv` is the table of good and bad values that `selftest.py`
+    runs.
 - `idcheck.py <outdir> <ids.json>` parses every stream namespace-aware and
   checks `xml:id` syntax and uniqueness per stream (ODF 1.2 Part 1 §2.2.1
   D.1, D.2). `jing -i` does not check ID uniqueness.
diff --git a/test/odf-validate/formula-cases.tsv 
b/test/odf-validate/formula-cases.tsv
new file mode 100644
index 0000000000..ca3ec01dca
--- /dev/null
+++ b/test/odf-validate/formula-cases.tsv
@@ -0,0 +1,172 @@
+# status<TAB>attribute<TAB>value<TAB>message substring. status: ok, or 
level:rule[+level:rule] (exact findings). attribute@text: outside 
office:spreadsheet.
+# values are the attribute text, with \n for a line feed and \u00a0 for a 
no-break space; the first rule's message must contain the substring.
+ok     table:formula   of:=1   
+ok     table:formula   of:=1+2*3       
+ok     table:formula   of:==1  
+ok     table:formula   of:=-1  
+ok     table:formula   of:=+1  
+ok     table:formula   of:=--1 
+ok     table:formula   of:=1%  
+ok     table:formula   of:=1%% 
+ok     table:formula   of:=2^3^2       
+ok     table:formula   of:=(1+2)*3     
+ok     table:formula   of:= 1 + 2      
+ok     table:formula   of:=.5  
+ok     table:formula   of:=1E5 
+ok     table:formula   of:=1.5e-3      
+ok     table:formula   of:="a""b"      
+ok     table:formula   of:=""  
+ok     table:formula   of:=SUM([.A1:.B2])      
+ok     table:formula   of:=SUM([.A1:.B2];3)    
+ok     table:formula   of:=TRUE()      
+ok     table:formula   of:=IF([.A1]>1;"x";"y") 
+ok     table:formula   of:=SUM(;)      
+ok     table:formula   of:=SUM(1;)     
+ok     table:formula   of:=SUM( )      
+ok     table:formula   of:=[.A1]       
+ok     table:formula   of:=[$Sheet1.A1]        
+ok     table:formula   of:=[$Sheet1.$A$1]      
+ok     table:formula   of:=['Sheet 1'.A1]      
+ok     table:formula   of:=[$'Sheet 1'.A1:.B2] 
+ok     table:formula   of:=[.A:.A]     
+ok     table:formula   of:=[.1:.1]     
+ok     table:formula   of:=[$Sheet1.A1:$Sheet2.B2]     
+ok     table:formula   of:=[.A1:.B2]![.B1:.C3] 
+ok     table:formula   of:=[.A1]~[.B1] 
+ok     table:formula   of:=[.A1]:[.B2] 
+ok     table:formula   of:=['file:///x.ods'#$Sheet1.A1]        
+ok     table:formula   of:=[#REF!]     
+ok     table:formula   of:=#N/A        
+ok     table:formula   of:=#DIV/0!     
+ok     table:formula   of:=#NAME?      
+ok     table:formula   of:=#NULL!      
+ok     table:formula   of:={1;2|3;4}   
+ok     table:formula   of:={"a";"b"}   
+ok     table:formula   of:=myname      
+ok     table:formula   of:=$$myname    
+ok     table:formula   of:=$$'my name' 
+ok     table:formula   of:='Sheet1'.myname     
+ok     table:formula   of:='file:///x.ods'#myname      
+ok     table:formula   of:='file:///x.ods'#$'S'.name   
+ok     table:formula   of:='label'     
+ok     table:formula   of:='a'!!'b'    
+ok     table:formula   of:=SUM('label')        
+ok     table:formula   of:=ORG.OPENOFFICE.STYLE(1)     
+ok     table:formula   of:=COM.MICROSOFT.X(1)  
+ok     table:formula   =1+2    
+ok     table:formula   1+2     
+ok     table:formula   SUM([.A1])      
+ok     table:formula   of:=SUM( [.A1] ; 2 )    
+ok     table:formula   of:=1 +2        
+ok     table:formula   of:= 1  
+ok     table:formula   of:=1\n+2       
+ok     table:formula   of:=[.A1]<>[.B1]        
+ok     table:formula   of:=[.A1]<=[.B1]        
+ok     table:formula   of:=[.A1]>=[.B1]        
+ok     table:formula   of:=2*[.A1]     
+ok     table:formula   of:=[.A1]&"x"   
+ok     table:formula   of:=ERROR.TYPE(1)       
+ok     table:formula   of:=Ünï+1       
+shall:formula-syntax   table:formula   of:=    expression expected
+shall:formula-syntax   table:formula   of:=1+  expression expected
+shall:formula-syntax   table:formula   of:=*1  unexpected '*'
+shall:formula-syntax   table:formula   of:=1 2 unexpected '2'
+shall:formula-syntax   table:formula   of:=SUM(1,2)    ')' expected
+shall:formula-syntax   table:formula   of:=SUM(1;2     ')' expected
+shall:formula-syntax   table:formula   of:=SUM (1)     whitespace between 
function name
+shall:formula-syntax   table:formula   of:=A1  cell reference
+shall:formula-syntax   table:formula   of:=TRUE        not an identifier
+shall:formula-syntax   table:formula   of:=5.  unexpected '.'
+shall:formula-syntax   table:formula   of:=1e  unexpected 'e'
+shall:formula-syntax   table:formula   of:="abc        unterminated string
+shall:formula-syntax   table:formula   of:=[.A1        without ']'
+shall:formula-syntax   table:formula   of:=[A1]        malformed reference
+shall:formula-syntax   table:formula   of:=[.a1]       malformed reference
+shall:formula-syntax   table:formula   of:=[.A0]       malformed reference
+shall:formula-syntax   table:formula   of:=[Sheet 1.A1]        malformed 
reference
+shall:formula-syntax   table:formula   of:=[.A1:B2]    malformed reference
+shall:formula-syntax   table:formula   
of:=[$Sheet1.$#REF!$#REF!:.$#REF!$#REF!]        malformed reference
+shall:formula-syntax   table:formula   of:=#ref!       malformed error constant
+shall:formula-syntax   table:formula   of:=#REF        malformed error constant
+shall:formula-syntax   table:formula   of:={}  unexpected '}'
+shall:formula-syntax   table:formula   of:=(1  ')' expected
+shall:formula-syntax   table:formula   of:=1)  unexpected ')'
+shall:formula-syntax   table:formula   of:=[.A1];[.B1] unexpected ';'
+shall:formula-syntax   table:formula   of:=-   expression expected
+shall:formula-syntax   table:formula   of:=1 % 2       unexpected '2'
+shall:formula-syntax   table:formula   of:=$A1 '$' must be
+shall:formula-syntax   table:formula   of:='abc'.      identifier expected
+shall:formula-syntax   table:formula   of:=1\u00a0+2   unexpected
+shall:formula-syntax   table:formula   of:='a b'#myname        source: not an 
IRI reference
+shall:formula-syntax   table:formula   of:=a.b unexpected '.'
+shall:formula-prefix-unbound   table:formula   zz:=1   not bound
+shall:formula-namespace        table:formula   oooc:=1 bound to 
<http://openoffice.org/2004/calc>
+should:formula-foreign-namespace       table:formula@text      oooc:=1 bound 
to <http://openoffice.org/2004/calc>
+ok     table:formula@text      of:=1+1 
+should:formula-function-unprefixed     table:formula   of:=FOO(1)      not a 
function of Part 2
+should:formula-function-case   table:formula   of:=sum(1)      upper case
+should:formula-array-ragged    table:formula   of:={1;2|3}     rows have 2/1 
columns
+ok     table:expression        [.A1]   
+ok     table:expression        of:=[.A1]       
+ok     table:expression        =[.A1]  
+ok     table:expression        [$Sheet1.$A$1:.$B$2]~[$Sheet1.$D$1:.$E$2]       
+shall:formula-syntax   table:expression        of:[.A1]        must start with 
'='
+shall:formula-syntax   table:expression        [.A1];[.B1]     unexpected ';'
+ok     text:formula    of:=1+1 
+ok     text:formula    Table+1 
+should:formula-foreign-namespace       text:formula    ooow:Table+1    bound 
to <http://openoffice.org/2004/writer>
+shall:formula-syntax   text:formula    of:=1+  expression expected
+ok     style:condition value()>=0      
+ok     style:condition value()=1       
+ok     style:condition value()!=true   
+ok     style:condition value()<-1.5    
+ok     style:condition footnote()      
+ok     style:condition endnote()       
+ok     style:condition header()        
+ok     style:condition footer()        
+ok     style:condition section()       
+ok     style:condition table() 
+ok     style:condition table-header()  
+ok     style:condition list-level()=2  
+ok     style:condition outline-level()=1       
+ok     style:condition cell-content()>5        
+ok     style:condition cell-content()="a"      
+ok     style:condition cell-content()>[.A1]    
+ok     style:condition cell-content-is-between(1,2)    
+ok     style:condition cell-content-is-not-between([.A1],[.B1])        
+ok     style:condition cell-content-is-between("a,b","c")      
+ok     style:condition is-true-formula([.A1]>1)        
+ok     style:condition of:is-true-formula(SUM([.A1];2)>1)      
+ok     style:condition is-true-formula(IF(1;2;3))      
+ok     style:condition cell-content() >= 5     
+ok     style:condition cell-content-is-between( 1 , 2 )        
+shall:formula-syntax   style:condition value() relational operator expected
+shall:formula-syntax   style:condition value()>        number, true or false 
expected
+shall:formula-syntax   style:condition list-level()=0  positive integer 
expected
+shall:formula-syntax   style:condition list-level()=x  positive integer 
expected
+shall:formula-syntax   style:condition footnote        '(' expected
+shall:formula-syntax   style:condition cell-content()<>5       unexpected '>'
+shall:formula-syntax   style:condition cell-content-is-between(1;2)    ',' 
expected
+shall:formula-syntax   style:condition cell-content-is-between(1)      ',' 
expected
+shall:formula-syntax   style:condition bogus() unknown condition 'bogus'
+shall:formula-syntax   style:condition footnote() and header() unexpected 'a'
+shall:formula-syntax   style:condition is-true-formula()       unexpected ')'
+shall:formula-syntax   style:condition cell-content()>5)       unexpected ')'
+ok     table:condition of:cell-content-text-length()<0 
+ok     table:condition cell-content-text-length()>=5   
+ok     table:condition cell-content-text-length-is-between(1,5)        
+ok     table:condition cell-content-text-length-is-not-between(1,5)    
+ok     table:condition cell-content-is-in-list("a";"b")        
+ok     table:condition cell-content-is-in-list("a")    
+ok     table:condition cell-content-is-in-list([.A1:.A5])      
+ok     table:condition cell-content-is-whole-number() and 
cell-content-is-between(1,5) 
+ok     table:condition cell-content-is-decimal-number() and 
cell-content()>=1.5        
+ok     table:condition cell-content-is-date() and is-true-formula([.A1]>0)     
+ok     table:condition cell-content-is-time() and 
cell-content-is-not-between(1,2)     
+ok     table:condition is-true-formula([.A1]>0)        
+shall:formula-syntax   table:condition cell-content()>5        unknown 
condition 'cell-content'
+shall:formula-syntax   table:condition cell-content-is-whole-number()  'and' 
expected
+shall:formula-syntax   table:condition cell-content-is-whole-number() and 
footnote()   value condition expected
+shall:formula-syntax   table:condition cell-content-is-in-list()       
unexpected ')'
+shall:formula-syntax   table:condition  and cell-content()>1   unknown 
condition 'and'
+shall:formula-syntax   table:condition cell-content-text-length()      
relational operator expected
diff --git a/test/odf-validate/formulacheck.py 
b/test/odf-validate/formulacheck.py
new file mode 100644
index 0000000000..0d23bd4caa
--- /dev/null
+++ b/test/odf-validate/formulacheck.py
@@ -0,0 +1,585 @@
+#**************************************************************
+#
+#  Licensed to the Apache Software Foundation (ASF) under one
+#  or more contributor license agreements.  See the NOTICE file
+#  distributed with this work for additional information
+#  regarding copyright ownership.  The ASF licenses this file
+#  to you under the Apache License, Version 2.0 (the
+#  "License"); you may not use this file except in compliance
+#  with the License.  You may obtain a copy of the License at
+#
+#    http://www.apache.org/licenses/LICENSE-2.0
+#
+#  Unless required by applicable law or agreed to in writing,
+#  software distributed under the License is distributed on an
+#  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+#  KIND, either express or implied.  See the License for the
+#  specific language governing permissions and limitations
+#  under the License.
+#
+#**************************************************************
+
+# usage: formulacheck.py <outdir> <formulas.json>
+# ODF 1.2 Part 1 2.2.1 D.3 and 2.2.4 D/E: the style:condition, 
table:condition, table:expression,
+# table:formula and text:formula values are parsed with the OpenFormula 
grammar (Part 2 chapter 5)
+# and the condition syntaxes of Part 1 19.468 and 19.596. Syntax only: no 
semantics, no function arity.
+# Findings are "shall: [rule] ..." or "should: [rule] ...".
+import sys, os, re, json, zipfile, collections, unicodedata
+from xml.parsers import expat
+
+D = os.path.dirname(os.path.abspath(__file__))
+OF = "urn:oasis:names:tc:opendocument:xmlns:of:1.2"
+NS = "urn:oasis:names:tc:opendocument:xmlns:%s:1.0"
+ATTRS = {NS % "style" + " condition": "style:condition", NS % "table" + " 
condition": "table:condition",
+         NS % "table" + " expression": "table:expression", NS % "table" + " 
formula": "table:formula",
+         NS % "text" + " formula": "text:formula"}
+SPREADSHEET = NS % "office" + " spreadsheet"
+STANDARD = {l.strip().upper() for l in open(os.path.join(D, 
"openformula-functions.txt")) if l.strip() and not l.startswith("#")}
+
+
+class Bad(Exception):
+    def __init__(self, pos, msg):
+        Exception.__init__(self, msg)
+        self.pos, self.msg = pos, msg
+
+
+def is_letter(c):
+    return unicodedata.category(c)[0] == "L" or unicodedata.category(c) == "Nl"
+
+
+def is_namechar(c):
+    return is_letter(c) or unicodedata.category(c) in ("Nd", "Mn", "Mc", "Me") 
or c == "_"
+
+
+# Part 2 5.3, 5.4, 5.8, 5.12
+NUMBER = re.compile(r"(?:[0-9]+(?:\.[0-9]+)?|\.[0-9]+)(?:[eE][-+]?[0-9]+)?")
+STRING = re.compile(r'"(?:[^"\x00]|"")*"')
+SINGLE = re.compile(r"'(?:[^']|'')+'")
+ERROR = re.compile(r"#[A-Z0-9]+(?:[!?]|/(?:[A-Z]|[0-9][!?]))")
+_Q = r"\$?'(?:[^']|'')+'"
+_SN = r"(?:%s|\$?[^\]\. #$']+)" % _Q
+_COL, _ROW = r"\$?[A-Z]+", r"\$?[1-9][0-9]*"
+_CELL = _COL + _ROW
+_SL = r"%s(?:\.(?:%s|%s))*" % (_SN, _CELL, _Q)
+RANGE = re.compile("|".join([
+    r"(?:%s)?\.%s(?::\.%s)?" % (_SL, _CELL, _CELL), r"(?:%s)?\.%s:\.%s" % 
(_SL, _COL, _COL), r"(?:%s)?\.%s:\.%s" % (_SL, _ROW, _ROW),
+    r"%s\.%s:%s\.%s" % (_SL, _CELL, _SL, _CELL), r"%s\.%s:%s\.%s" % (_SL, 
_COL, _SL, _COL), r"%s\.%s:%s\.%s" % (_SL, _ROW, _SL, _ROW)]))
+QSHEET = re.compile(_Q)
+CELLREF = re.compile(r"[A-Za-z]+[0-9]+")
+INFIX = ("<>", "<=", ">=", "+", "-", "*", "/", "^", "=", "<", ">", "&", ":", 
"!", "~")
+# Part 1 19.468, 19.596
+DECIMAL = re.compile(r"[-+]?(?:[0-9]+(?:\.[0-9]*)?|\.[0-9]+)")
+RELOP = re.compile(r"<=|>=|!=|<|>|=")
+CNAME = re.compile(r"[a-z][a-z-]*")
+
+
+def check_iri(iri):
+    """Approximates the RFC 3987 IRI-reference rule: no controls, spaces or 
the characters excluded from IRIs."""
+    if re.search(r'[\x00-\x20<>"{}|\\^`\x7f]', iri) or 
re.search(r"%(?![0-9A-Fa-f]{2})", iri):
+        raise ValueError("not an IRI reference")
+
+
+class Parser:
+    def __init__(self, text):
+        self.s, self.i, self.funcs, self.ragged = text, 0, [], None
+
+    def fail(self, msg, at=None):
+        raise Bad(self.i if at is None else at, msg)
+
+    def at(self, chars):
+        return self.i < len(self.s) and self.s[self.i] in chars
+
+    def ws(self):
+        while self.at(" \t\n\r"):
+            self.i += 1
+
+    def expect(self, ch):
+        if not self.at(ch):
+            self.fail("%r expected" % ch)
+        self.i += 1
+
+    def end(self):
+        if self.i < len(self.s):
+            self.fail("unexpected %r" % self.s[self.i])
+
+    # 5.2: Formula ::= Intro? Expression
+    def formula(self):
+        if self.at("="):
+            self.i += 1
+            if self.at("="):
+                self.i += 1
+        self.expression()
+        self.end()
+
+    def expression(self):
+        self.ws()
+        self.operand()
+        while True:
+            self.ws()
+            op = next((o for o in INFIX if self.s.startswith(o, self.i)), None)
+            if op is None:
+                return
+            self.i += len(op)
+            self.ws()
+            self.operand()
+
+    def operand(self):
+        while self.at("+-"):
+            self.i += 1
+            self.ws()
+        self.primary()
+        while True:
+            j = self.i
+            self.ws()
+            if self.at("%"):
+                self.i += 1
+            else:
+                self.i = j
+                return
+
+    def primary(self):
+        s, i = self.s, self.i
+        if i >= len(s):
+            self.fail("expression expected")
+        c = s[i]
+        m = NUMBER.match(s, i) if (c in "0123456789.") else None
+        if m:
+            self.i = m.end()
+        elif c == '"':
+            m = STRING.match(s, i)
+            if not m:
+                self.fail("unterminated string")
+            self.i = m.end()
+        elif c == "{":
+            self.array()
+        elif c == "(":
+            self.i += 1
+            self.expression()
+            self.expect(")")
+        elif c == "[":
+            self.reference()
+        elif c == "#":
+            m = ERROR.match(s, i)
+            if not m:
+                self.fail("malformed error constant")
+            self.i = m.end()
+        elif c in "'$":
+            self.quoted_or_named()
+        elif is_letter(c):
+            self.name()
+        else:
+            self.fail("unexpected %r" % c)
+
+    def array(self):
+        self.i += 1
+        widths = []
+        while True:
+            n = 1
+            self.expression()
+            while self.at(";"):
+                self.i += 1
+                self.expression()
+                n += 1
+            widths.append(n)
+            if self.at("|"):
+                self.i += 1
+                continue
+            break
+        self.expect("}")
+        if len(set(widths)) > 1:
+            self.ragged = widths
+
+    def name(self):
+        s, j = self.s, self.i
+        k = j
+        while k < len(s) and (is_namechar(s[k]) or s[k] == "."):
+            k += 1
+        if k < len(s) and s[k] == "(":
+            self.funcs.append(s[j:k])
+            self.i = k + 1
+            self.params()
+            return
+        ident = s[j:k].split(".", 1)[0]
+        self.i = j + len(ident)
+        if self.i == k and re.match(r"[ \t\n\r]+\(", s[k:]):
+            self.fail("whitespace between function name and '('")
+        self.check_identifier(ident)
+
+    def check_identifier(self, ident):
+        if CELLREF.fullmatch(ident):
+            self.fail("%r looks like a cell reference; references are written 
in square brackets" % ident, self.i - len(ident))
+        if ident.lower() in ("true", "false"):
+            self.fail("%r is not an identifier; use %s()" % (ident, 
ident.upper()), self.i - len(ident))
+
+    def params(self):
+        while True:
+            self.ws()
+            if self.at(");"):
+                pass
+            else:
+                self.expression()
+                self.ws()
+            if self.at(";"):
+                self.i += 1
+                continue
+            self.expect(")")
+            return
+
+    # 5.8, 5.10, 5.11
+    def source(self):
+        """Source ::= "'" IRI "'" "#" at the current position, if there is 
one."""
+        m = SINGLE.match(self.s, self.i)
+        if m and self.s.startswith("#", m.end()):
+            try:
+                check_iri(m.group(0)[1:-1].replace("''", "'"))
+            except ValueError as e:
+                self.fail("source: %s" % e)
+            self.i = m.end() + 1
+            return True
+        return False
+
+    def simple_named(self):
+        """SimpleNamedExpression ::= Identifier | '$$' (Identifier | 
SingleQuoted)"""
+        if self.s.startswith("$$", self.i):
+            self.i += 2
+            m = SINGLE.match(self.s, self.i)
+            if m:
+                self.i = m.end()
+                return
+        self.identifier()
+
+    def quoted_or_named(self):
+        s = self.s
+        if self.source():
+            m = QSHEET.match(s, self.i)
+            if m and s.startswith(".", m.end()):
+                self.i = m.end() + 1
+            self.simple_named()
+            return
+        if s.startswith("$$", self.i):
+            self.simple_named()
+            return
+        m = QSHEET.match(s, self.i)
+        if not m:
+            self.fail("'$' must be '$$' or precede a quoted sheet name")
+        j = m.end()
+        k = j
+        while k < len(s) and s[k] in " \t\n\r":
+            k += 1
+        if s.startswith(".", j):
+            self.i = j + 1
+            self.simple_named()
+        elif s[self.i] != "$" and s.startswith("!!", k):
+            self.i = k + 2
+            self.ws()
+            m = SINGLE.match(s, self.i)
+            if not m:
+                self.fail("quoted label expected after '!!'")
+            self.i = m.end()
+        elif s[self.i] == "$":
+            self.fail("a quoted sheet name must be followed by '.'")
+        else:
+            self.i = j
+
+    def identifier(self):
+        s, j = self.s, self.i
+        if j >= len(s) or not is_letter(s[j]):
+            self.fail("identifier expected")
+        k = j
+        while k < len(s) and is_namechar(s[k]):
+            k += 1
+        self.i = k
+        self.check_identifier(s[j:k])
+
+    def reference(self):
+        s = self.s
+        j = self.i + 1
+        k = j
+        while k < len(s) and s[k] != "]":
+            if s[k] == "'":
+                m = SINGLE.match(s, k)
+                if not m:
+                    self.fail("unterminated quote in reference")
+                k = m.end()
+            else:
+                k += 1
+        if k >= len(s):
+            self.fail("reference without ']'", self.i)
+        inner = s[j:k]
+        sub = Parser(inner)
+        sub.i = 0
+        try:
+            if inner == "#REF!":
+                pass
+            else:
+                sub.source()
+                if not RANGE.fullmatch(inner, sub.i):
+                    raise Bad(j + sub.i, "malformed reference [%s]" % inner)
+        except Bad as e:
+            raise Bad(j + e.pos if e.pos < len(inner) else j, e.msg)
+        self.i = k + 1
+
+    # Part 1 19.468 / 19.596 value, condition
+    def value(self):
+        s, save = self.s, self.i
+        for rx in (DECIMAL, STRING):
+            m = rx.match(s, save)
+            if m:
+                j = m.end()
+                while j < len(s) and s[j] in " \t\n\r":
+                    j += 1
+                if j >= len(s) or s[j] in ",)":
+                    self.i = j
+                    return
+        self.i = save
+        self.expression()
+
+    def cname(self):
+        self.ws()
+        m = CNAME.match(self.s, self.i)
+        if not m:
+            self.fail("condition expected")
+        self.i = m.end()
+        return m.group(0)
+
+    def call(self, nargs):
+        """'(' value {',' value} ')' or '()'"""
+        self.expect("(")
+        self.ws()
+        if nargs == 0:
+            self.expect(")")
+            return
+        for n in range(nargs):
+            if n:
+                self.ws()
+                self.expect(",")
+            self.ws()
+            self.value()
+        self.ws()
+        self.expect(")")
+
+    def op_value(self):
+        self.ws()
+        if not RELOP.match(self.s, self.i):
+            self.fail("relational operator expected")
+        self.i = RELOP.match(self.s, self.i).end()
+        self.ws()
+        self.value()
+
+    def style_condition(self):
+        name = self.cname()
+        if name in ("footnote", "endnote", "header", "footer", "section", 
"table", "table-header"):
+            self.call(0)
+        elif name in ("list-level", "outline-level"):
+            self.call(0)
+            self.ws()
+            self.expect("=")
+            self.ws()
+            m = re.compile(r"\+?0*[1-9][0-9]*").match(self.s, self.i)
+            if not m:
+                self.fail("positive integer expected")
+            self.i = m.end()
+        elif name == "value":
+            self.call(0)
+            self.ws()
+            m = RELOP.match(self.s, self.i)
+            if not m:
+                self.fail("relational operator expected")
+            self.i = m.end()
+            self.ws()
+            m = re.compile(r"true|false").match(self.s, self.i) or 
DECIMAL.match(self.s, self.i)
+            if not m:
+                self.fail("number, true or false expected")
+            self.i = m.end()
+        elif name == "cell-content":
+            self.call(0)
+            self.op_value()
+        elif name in ("cell-content-is-between", 
"cell-content-is-not-between"):
+            self.call(2)
+        elif name == "is-true-formula":
+            self.call_expression()
+        else:
+            self.fail("unknown condition %r" % name, self.i - len(name))
+        self.ws()
+        self.end()
+
+    def call_expression(self):
+        self.expect("(")
+        self.expression()
+        self.expect(")")
+
+    def value_condition(self):
+        name = self.cname()
+        if name == "cell-content":
+            self.call(0)
+            self.op_value()
+        elif name in ("cell-content-is-between", 
"cell-content-is-not-between"):
+            self.call(2)
+        elif name == "is-true-formula":
+            self.call_expression()
+        else:
+            self.fail("value condition expected, found %r" % name, self.i - 
len(name))
+
+    def string_list(self):
+        """list of string entries separated by ';'; leaves the position before 
')'"""
+        s, j = self.s, self.i
+        while True:
+            m = STRING.match(s, j)
+            if not m:
+                return False
+            k = m.end()
+            while k < len(s) and s[k] in " \t\n\r":
+                k += 1
+            if s.startswith(";", k):
+                j = k + 1
+                while j < len(s) and s[j] in " \t\n\r":
+                    j += 1
+            elif s.startswith(")", k):
+                self.i = k
+                return True
+            else:
+                return False
+
+    def validation_condition(self):
+        name = self.cname()
+        if name == "cell-content-text-length":
+            self.call(0)
+            self.op_value()
+        elif name in ("cell-content-text-length-is-between", 
"cell-content-text-length-is-not-between"):
+            self.call(2)
+        elif name == "cell-content-is-in-list":
+            self.expect("(")
+            self.ws()
+            if not self.string_list():
+                self.expression()
+            self.expect(")")
+        elif name in ("cell-content-is-decimal-number", 
"cell-content-is-whole-number", "cell-content-is-date", "cell-content-is-time"):
+            self.call(0)
+            m = re.compile(r"\s+and\s+").match(self.s, self.i)
+            if not m:
+                self.fail("'and' expected")
+            self.i = m.end()
+            self.value_condition()
+        elif name == "is-true-formula":
+            self.call_expression()
+        else:
+            self.fail("unknown condition %r" % name, self.i - len(name))
+        self.ws()
+        self.end()
+
+
+def check_value(attr, body, has_prefix):
+    """Returns (None | (pos, message), [function names], ragged column counts 
| None)."""
+    p = Parser(body)
+    try:
+        if attr in ("table:formula", "text:formula"):
+            p.formula()
+        elif attr == "table:expression":
+            if has_prefix and not body.startswith("="):
+                raise Bad(0, "an expression with a namespace prefix must start 
with '=' (19.635)")
+            p.formula()
+        elif attr == "style:condition":
+            p.style_condition()
+        else:
+            p.validation_condition()
+    except Bad as e:
+        return (e.pos, e.msg), p.funcs, p.ragged
+    return None, p.funcs, p.ragged
+
+
+NCPREFIX = re.compile(r"([^\W\d][\w.\-]*):")
+
+
+def check_stream(data, stats):
+    """Returns [(line, col, level, rule, message)]."""
+    out = []
+    scope = collections.defaultdict(list)
+    state = {"depth": 0, "ss": None}
+    p = expat.ParserCreate(namespace_separator=" ")
+    p.StartNamespaceDeclHandler = lambda pre, uri: scope[pre].append(uri)
+    p.EndNamespaceDeclHandler = lambda pre: scope[pre].pop()
+
+    def start(name, attrs):
+        state["depth"] += 1
+        if name == SPREADSHEET:
+            state["ss"] = state["depth"]
+        for key, value in attrs.items():
+            attr = ATTRS.get(key)
+            if attr is None:
+                continue
+            line, col = p.CurrentLineNumber, p.CurrentColumnNumber
+
+            def add(level, rule, msg):
+                out.append((line, col, level, rule, '%s="%s": %s' % (attr, 
value if len(value) < 80 else value[:77] + "...", msg)))
+            m = NCPREFIX.match(value)
+            body, has_prefix = value, False
+            if m:
+                pre = m.group(1)
+                uri = scope[pre][-1] if scope.get(pre) else None
+                stats[(attr, pre if uri is None else uri)] += 1
+                if uri is None:
+                    add("shall", "formula-prefix-unbound", "namespace prefix 
\"%s\" is not bound" % pre)
+                    continue
+                if uri != OF:
+                    if attr == "table:formula" and state["ss"] is not None:
+                        add("shall", "formula-namespace", "bound to <%s>, not 
to the of:1.2 namespace (2.2.4 D)" % uri)
+                    else:
+                        add("should", "formula-foreign-namespace", "bound to 
<%s>; the syntax is not defined by Part 2" % uri)
+                    continue
+                body, has_prefix = value[m.end():], True
+            else:
+                stats[(attr, "(none)")] += 1
+            err, funcs, ragged = check_value(attr, body, has_prefix)
+            if err:
+                pos, msg = err
+                add("shall", "formula-syntax", "%s at character %d" % (msg, 
pos + (len(value) - len(body)) + 1))
+            if ragged:
+                add("should", "formula-array-ragged", "inline array rows have 
%s columns (5.13)" % "/".join(map(str, ragged)))
+            for f in funcs:
+                if f.upper() not in STANDARD and "." not in f:
+                    add("should", "formula-function-unprefixed", "%s is not a 
function of Part 2 and has no origin prefix (5.7)" % f)
+                if f != f.upper():
+                    add("should", "formula-function-case", "%s: function names 
should be written in upper case (6.1)" % f)
+    p.StartElementHandler = start
+
+    def end(name):
+        if state["ss"] == state["depth"]:
+            state["ss"] = None
+        state["depth"] -= 1
+    p.EndElementHandler = end
+    try:
+        p.Parse(data, True)
+    except expat.ExpatError as e:
+        out.append((e.lineno, e.offset, "shall", "formula-stream", "not 
namespace-well-formed: " + expat.ErrorString(e.code)))
+    return out
+
+
+def main():
+    outdir, fjson = sys.argv[1], sys.argv[2]
+    if not os.path.abspath(fjson).startswith(os.path.join(D, "runs") + os.sep):
+        sys.exit("formulacheck.py: <formulas.json> must live under " + 
os.path.join(D, "runs"))
+    errors, bad, stats = [], [], collections.Counter()
+    pkgs = sorted(p for p in os.listdir(outdir) if not p.startswith("."))
+    for pkg in pkgs:
+        try:
+            z = zipfile.ZipFile(os.path.join(outdir, pkg))
+        except Exception:
+            bad.append(pkg); continue
+        for name in z.namelist():
+            if name.rsplit("/", 1)[-1] not in ("content.xml", "styles.xml", 
"settings.xml"):
+                continue
+            for line, col, level, rule, msg in check_stream(z.read(name), 
stats):
+                errors.append([pkg + "/" + name, line, col, "%s: [%s] %s" % 
(level, rule, msg)])
+    json.dump({"packages": pkgs, "bad": bad, "checked": {"%s %s" % k: v for k, 
v in sorted(stats.items())}, "errors": errors}, open(fjson, "w"))
+    shall = [e for e in errors if e[3].startswith("shall:")]
+    print(f"formula check: {sum(stats.values())} values checked; {len(shall)} 
shall and {len(errors) - len(shall)} should findings in "
+          f"{len({e[0].split('/')[0] for e in errors})} of {len(pkgs)} 
packages, unreadable: {bad}")
+    for (attr, ns), v in sorted(stats.items()):
+        print(f"  {v:6d} {attr} {ns}")
+    rules = collections.Counter(e[3].split("]")[0] + "]" for e in errors)
+    for k, v in sorted(rules.items()):
+        print(f"  {v:6d} {k}")
+
+
+if __name__ == "__main__":
+    main()
diff --git a/test/odf-validate/openformula-functions.txt 
b/test/odf-validate/openformula-functions.txt
new file mode 100644
index 0000000000..adac44e131
--- /dev/null
+++ b/test/odf-validate/openformula-functions.txt
@@ -0,0 +1,393 @@
+# Names of the functions defined in OpenDocument 1.2 Part 2 (OpenFormula) 
chapter 6, one per line.
+# Used by formulacheck.py to tell standard functions from nonstandard ones 
(Part 2 5.7).
+ABS
+ACCRINT
+ACCRINTM
+ACOS
+ACOSH
+ACOT
+ACOTH
+ADDRESS
+AMORLINC
+AND
+ARABIC
+AREAS
+ASC
+ASIN
+ASINH
+ATAN
+ATAN2
+ATANH
+AVEDEV
+AVERAGE
+AVERAGEA
+AVERAGEIF
+AVERAGEIFS
+BASE
+BESSELI
+BESSELJ
+BESSELK
+BESSELY
+BETADIST
+BETAINV
+BIN2DEC
+BIN2HEX
+BIN2OCT
+BINOM.DIST.RANGE
+BINOMDIST
+BITAND
+BITLSHIFT
+BITOR
+BITRSHIFT
+BITXOR
+CEILING
+CELL
+CHAR
+CHISQDIST
+CHISQINV
+CHOOSE
+CLEAN
+CODE
+COLUMN
+COLUMNS
+COMBIN
+COMBINA
+COMPLEX
+CONCATENATE
+CONFIDENCE
+CONVERT
+CORREL
+COS
+COSH
+COT
+COTH
+COUNT
+COUNTA
+COUNTBLANK
+COUNTIF
+COUNTIFS
+COUPDAYBS
+COUPDAYS
+COUPDAYSNC
+COUPNCD
+COUPNUM
+COUPPCD
+COVAR
+CRITBINOM
+CSC
+CSCH
+CUMIPMT
+CUMPRINC
+DATE
+DATEDIF
+DATEVALUE
+DAVERAGE
+DAY
+DAYS
+DAYS360
+DB
+DCOUNT
+DCOUNTA
+DDB
+DDE
+DEC2BIN
+DEC2HEX
+DEC2OCT
+DECIMAL
+DEGREES
+DELTA
+DEVSQ
+DGET
+DISC
+DMAX
+DMIN
+DOLLAR
+DOLLARDE
+DOLLARFR
+DPRODUCT
+DSTDEV
+DSTDEVP
+DSUM
+DURATION
+DVAR
+DVARP
+EDATE
+EFFECT
+EOMONTH
+ERF
+ERFC
+ERROR.TYPE
+EUROCONVERT
+EVEN
+EXACT
+EXP
+EXPONDIST
+FACT
+FACTDOUBLE
+FALSE
+FDIST
+FIND
+FINDB
+FINV
+FISHER
+FISHERINV
+FIXED
+FLOOR
+FORECAST
+FORMULA
+FREQUENCY
+FTEST
+FV
+FVSCHEDULE
+GAMMA
+GAMMADIST
+GAMMAINV
+GAMMALN
+GAUSS
+GCD
+GEOMEAN
+GESTEP
+GETPIVOTDATA
+GROWTH
+HARMEAN
+HEX2BIN
+HEX2DEC
+HEX2OCT
+HLOOKUP
+HOUR
+HYPERLINK
+HYPGEOMDIST
+IF
+IFERROR
+IFNA
+IMABS
+IMAGINARY
+IMARGUMENT
+IMCONJUGATE
+IMCOS
+IMCOT
+IMCSC
+IMCSCH
+IMDIV
+IMEXP
+IMLN
+IMLOG10
+IMLOG2
+IMPOWER
+IMPRODUCT
+IMREAL
+IMSEC
+IMSECH
+IMSIN
+IMSINH
+IMSQRT
+IMSUB
+IMSUM
+IMTAN
+INDEX
+INDIRECT
+INFO
+INT
+INTERCEPT
+INTRATE
+IPMT
+IRR
+ISBLANK
+ISERR
+ISERROR
+ISEVEN
+ISFORMULA
+ISLOGICAL
+ISNA
+ISNONTEXT
+ISNUMBER
+ISODD
+ISOWEEKNUM
+ISPMT
+ISREF
+ISTEXT
+JIS
+KURT
+LARGE
+LCM
+LEFT
+LEFTB
+LEGACY.CHIDIST
+LEGACY.CHIINV
+LEGACY.CHITEST
+LEGACY.FDIST
+LEGACY.FINV
+LEGACY.NORMSDIST
+LEGACY.NORMSINV
+LEGACY.TDIST
+LEN
+LENB
+LINEST
+LN
+LOG
+LOG10
+LOGEST
+LOGINV
+LOGNORMDIST
+LOOKUP
+LOWER
+MATCH
+MAX
+MAXA
+MDETERM
+MDURATION
+MEDIAN
+MID
+MIDB
+MIN
+MINA
+MINUTE
+MINVERSE
+MIRR
+MMULT
+MOD
+MODE
+MONTH
+MROUND
+MULTINOMIAL
+MULTIPLE.OPERATIONS
+MUNIT
+N
+NA
+NEGBINOMDIST
+NETWORKDAYS
+NOMINAL
+NORMDIST
+NORMINV
+NOT
+NOW
+NPER
+NPV
+NUMBERVALUE
+OCT2BIN
+OCT2DEC
+OCT2HEX
+ODD
+ODDFPRICE
+ODDFYIELD
+ODDLPRICE
+ODDLYIELD
+OFFSET
+OR
+PDURATION
+PEARSON
+PERCENTILE
+PERCENTRANK
+PERMUT
+PERMUTATIONA
+PHI
+PI
+PMT
+POISSON
+POWER
+PPMT
+PRICE
+PRICEDISC
+PRICEMAT
+PROB
+PRODUCT
+PROPER
+PV
+QUARTILE
+QUOTIENT
+RADIANS
+RAND
+RANDBETWEEN
+RANK
+RATE
+RECEIVED
+REPLACE
+REPLACEB
+REPT
+RIGHT
+RIGHTB
+ROMAN
+ROUND
+ROUNDDOWN
+ROUNDUP
+ROW
+ROWS
+RRI
+RSQ
+SEARCH
+SEARCHB
+SEC
+SECH
+SECOND
+SERIESSUM
+SHEET
+SHEETS
+SIGN
+SIN
+SINH
+SKEW
+SKEWP
+SLN
+SLOPE
+SMALL
+SQRT
+SQRTPI
+STANDARDIZE
+STDEV
+STDEVA
+STDEVP
+STDEVPA
+STEYX
+SUBSTITUTE
+SUBTOTAL
+SUM
+SUMIF
+SUMIFS
+SUMPRODUCT
+SUMSQ
+SUMX2MY2
+SUMX2PY2
+SUMXMY2
+SYD
+T
+TAN
+TANH
+TBILLEQ
+TBILLPRICE
+TBILLYIELD
+TEXT
+TIME
+TIMEVALUE
+TINV
+TODAY
+TRANSPOSE
+TREND
+TRIM
+TRIMMEAN
+TRUE
+TRUNC
+TTEST
+TYPE
+UNICHAR
+UNICODE
+UPPER
+VALUE
+VAR
+VARA
+VARP
+VARPA
+VDB
+VLOOKUP
+WEEKDAY
+WEEKNUM
+WEIBULL
+WORKDAY
+XIRR
+XNPV
+XOR
+YEAR
+YEARFRAC
+YIELD
+YIELDDISC
+YIELDMAT
+ZTEST
diff --git a/test/odf-validate/selftest.py b/test/odf-validate/selftest.py
index f5e1c1183a..e63bcc77ca 100644
--- a/test/odf-validate/selftest.py
+++ b/test/odf-validate/selftest.py
@@ -20,7 +20,7 @@
 #**************************************************************
 
 # usage: selftest.py -- validator negative controls; exits 1 if any control 
misbehaves
-import json, os, subprocess, sys
+import collections, json, os, subprocess, sys
 D = os.path.dirname(os.path.abspath(__file__))
 val = os.path.join(D, "runs", "control", "val.json")
 os.makedirs(os.path.dirname(val), exist_ok=True)
@@ -65,6 +65,33 @@ dexpected = json.load(open(os.path.join(ddir, 
".expected.json")))
 def dfound(doc): return sorted({tuple(m.split("]")[0].split(": [")) for m in 
(e[3] for e in de if e[0].startswith(doc + "/"))})
 for doc in sorted(dexpected):
     checks["doctypecheck: " + doc + " reports exactly " + (", ".join(r for _, 
r in dexpected[doc]) or "nothing")] = dfound(doc) == [tuple(x) for x in 
dexpected[doc]]
+import importlib.util
+from xml.sax.saxutils import quoteattr
+spec = importlib.util.spec_from_file_location("formulacheck", os.path.join(D, 
"formulacheck.py"))
+fc = importlib.util.module_from_spec(spec)
+spec.loader.exec_module(fc)
+cases = [l.rstrip("\n").split("\t") for l in open(os.path.join(D, 
"formula-cases.tsv"), encoding="utf-8") if not l.startswith("#") and l.strip()]
+NSDECL = ('xmlns:office="urn:oasis:names:tc:opendocument:xmlns:office:1.0" 
xmlns:table="urn:oasis:names:tc:opendocument:xmlns:table:1.0" '
+          'xmlns:style="urn:oasis:names:tc:opendocument:xmlns:style:1.0" 
xmlns:text="urn:oasis:names:tc:opendocument:xmlns:text:1.0" '
+          'xmlns:of="urn:oasis:names:tc:opendocument:xmlns:of:1.2" 
xmlns:oooc="http://openoffice.org/2004/calc"; 
xmlns:ooow="http://openoffice.org/2004/writer";')
+lines = ["<office:document-content %s><office:body>" % NSDECL]
+for st, attr, val, msg in cases:
+    val = val.replace("\\n", "\n").replace("\\u00a0", "\u00a0")
+    name, _, ctx = attr.partition("@")
+    tag = "office:text" if ctx == "text" else "office:spreadsheet"
+    lines.append("<%s><table:table-cell %s=%s/></%s>" % (tag, name, 
quoteattr(val, {"\n": "&#10;"}), tag))
+lines.append("</office:body></office:document-content>")
+got = collections.defaultdict(list)
+for line, col, level, rule, m in 
fc.check_stream("\n".join(lines).encode("utf-8"), collections.Counter()):
+    got[line - 2].append((level + ":" + rule, m))
+bad_cases = []
+for n, (st, attr, val, msg) in enumerate(cases):
+    want = [] if st == "ok" else st.split("+")
+    if sorted(r for r, _ in got[n]) != sorted(want) or (msg and not any(msg in 
m for _, m in got[n])):
+        bad_cases.append((n, st, attr, val, got[n]))
+for c in bad_cases[:10]:
+    print("CASE", *c)
+checks["formulacheck: all %d cases in formula-cases.tsv give exactly the 
expected findings" % len(cases)] = not bad_cases and len(cases) > 100
 for k, ok in checks.items():
     print(("PASS " if ok else "FAIL ") + k)
 sys.exit(0 if all(checks.values()) else 1)

Reply via email to