10.12., 9:00 - 11:00: Due to updates GitLab may be unavailable for some minutes between 09:00 and 11:00.

Commit 968f1acc authored by Eckhart Arnold's avatar Eckhart Arnold

- LaTeX ebnf erweitert

parent 7cba2407
......@@ -51,9 +51,10 @@ https://bitbucket.org/apalala/grako
import abc
import copy
from functools import partial
import os
import platform
from functools import partial
try:
import regex as re
except ImportError:
......@@ -206,7 +207,6 @@ def add_parser_guard(parser_func):
node, rest = parser.visited.get(location, (None, rest))
# don't overwrite any positive match (i.e. node not None) in the cache
# and don't add empty entries for parsers returning from left recursive calls!
# TODO: Bei Gelegenheit messen, ob sich das hier überhaupt lohnt...
if node is None and not grammar.left_recursion_encountered__:
# ortherwise also cache None-results
parser.visited[location] = None, rest
......
......@@ -32,7 +32,7 @@ except ImportError:
from .typing34 import AbstractSet, Any, ByteString, Callable, cast, Container, Dict, \
Iterator, List, NamedTuple, Sequence, Union, Text, Tuple
from DHParser.toolkit import log_dir, repr_call, expand_table, line_col, smart_list
from DHParser.toolkit import log_dir, expand_table, line_col, smart_list
__all__ = ['WHITESPACE_PTYPE',
......@@ -203,17 +203,17 @@ class Node:
"""Initializes the ``Node``-object with the ``Parser``-Instance
that generated the node and the parser's result.
"""
self._result = '' # type: StrictResultType
# self._result = '' # type: StrictResultType
# self._children = () # type: ChildrenType
self._errors = [] # type: List[str]
self._children = () # type: ChildrenType
self.result = result
self._len = len(self.result) if not self.children else \
sum(child._len for child in self.children) # type: int
# self.pos: int = 0 # continuous updating of pos values
self._len = len(result) if not self._children else \
sum(child._len for child in self._children) # type: int
# self.pos: int = 0 # continuous updating of pos values wastes a lot of time
self._pos = -1 # type: int
self.parser = parser or ZOMBIE_PARSER
self.error_flag = any(r.error_flag for r in self.children) \
if self.children else False # type: bool
self.error_flag = any(r.error_flag for r in self._children) \
if self._children else False # type: bool
def __str__(self):
if self.children:
......@@ -254,9 +254,9 @@ class Node:
# assert ((isinstance(result, tuple) and all(isinstance(child, Node) for child in result))
# or isinstance(result, Node)
# or isinstance(result, str)), str(result)
self._result = (result,) if isinstance(result, Node) else result or ''
self._result = (result,) if isinstance(result, Node) else result or '' # type: StrictResultType
self._children = cast(ChildrenType, self._result) \
if isinstance(self._result, tuple) else cast(ChildrenType, ())
if isinstance(self._result, tuple) else cast(ChildrenType, ()) # type: ChildrenType
@property
def children(self) -> ChildrenType:
......
......@@ -52,7 +52,7 @@ def selftest(file_name):
else:
# compile the grammar again using the result of the previous
# compilation as parser
for i in range(1):
for i in range(100):
result = compileDSL(grammar, nil_scanner, result, transformer, compiler)
print(result)
return result
......
# latex Grammar
# LaTeX-Grammar for DHParser
@ testing = True
@ whitespace = /[ \t]*(?:\n(?![ \t]*\n)[ \t]*)?/ # optional whitespace, including at most one linefeed
@ comment = /%.*(?:\n|$)/
latexdoc = preamble document
preamble = { command }+
document = [PARSEP] "\begin{document}" [PARSEP] frontpages [PARSEP] (chapters | sections)
[bibliography] [index] "\end{document}" §EOF
frontpages = sequence
latexdoc = preamble document
preamble = { command }+
document = [PARSEP] "\begin{document}" [PARSEP]
frontpages [PARSEP]
(chapters | sections) [PARSEP]
[bibliography] [index] [PARSEP]
"\end{document}" [PARSEP] §EOF
frontpages = sequence
#######################################################################
#
......@@ -17,26 +22,26 @@ frontpages = sequence
#
#######################################################################
Chapters = { Chapter [PARSEP] }+
Chapter = "\Chapter" block [PARSEP] { sequence | Sections }
Chapters = { Chapter [PARSEP] }+
Chapter = "\Chapter" block [PARSEP] { sequence | Sections }
Sections = { Section [PARSEP] }+
Section = "\Section" block [PARSEP] { sequence | SubSections }
Sections = { Section [PARSEP] }+
Section = "\Section" block [PARSEP] { sequence | SubSections }
SubSections = { SubSection [PARSEP] }+
SubSection = "\SubSection" block [PARSEP] { sequence | SubSubSections }
SubSections = { SubSection [PARSEP] }+
SubSection = "\SubSection" block [PARSEP] { sequence | SubSubSections }
SubSubsections = { SubSubSection [PARSEP] }+
SubSubSection = "\SubSubSection" block [PARSEP] { sequence | Paragraphs }
Paragraphs = { Paragraph [PARSEP] }+
Paragraph = "\paragraph" block [PARSEP] { sequence | SubParagraphs }
Paragraphs = { Paragraph [PARSEP] }+
Paragraph = "\paragraph" block [PARSEP] { sequence | SubParagraphs }
SubParagraphs = { SubParagraph [PARSEP] }+
SubParagraph = "\subparagpaph" block [PARSEP] { sequence }
SubParagraphs = { SubParagraph [PARSEP] }+
SubParagraph = "\subparagpaph" block [PARSEP] { sequence }
bibliography = "\bibliography" block [PARSEP]
index = "\printindex" [PARSEP
bibliography = "\bibliography" block [PARSEP]
index = "\printindex" [PARSEP
#######################################################################
......@@ -45,21 +50,53 @@ index = "\printindex" [PARSEP
#
#######################################################################
blockenv = beginenv sequence §endenv
parblock = /{/ sequence §/}/
sequence = { (paragraph | itemize | enumeration | figure | table | blockenv ) [PARSEP] }+
#### block environments ####
block_enrivonment = known_enrivonment | generic_enrivonment
known_enrivonment = itemize | enumerate | figure | table
generic_enrivonment = begin_enrivonment sequence §end_enrivonment
itemize = "\begin{itemize}" [PARSEP] { item } §"\end{itemize}"
enumerate = "\begin{enumerate}" [PARSEP] {item } §"end{enumerate}"
item = "\item" [PARSEP] sequence
#### paragraphs and sequences of paragraphs ####
block_of_paragraphs = /{/ sequence §/}/
sequence = { (paragraph | block_enrivonment ) [PARSEP] }+
paragraph = { !blockcmd textelements //~ }+
textelemts = command | text | block | inline_enrivonment
#### inline enivronments ####
inline_enrivonment = known_inline_env | generic_inline_env
known_inline_env = inline_math
generic_inline_env = begin_enrivonment { textelements }+ §end_enrivonment
begin_enrivonment = "\begin{" §NAME §"}"
end_enrivonment = "\end{" §::NAME §"}"
inline_math = "$" math_Text "$"
#### commands ####
paragraph = { !blockcmd textelements //~ }+
textelemts = command | text | block | inlinemath | inlineenv
command = knownd_command | generic_command
known_command = footnote
generic_command = CMDNAME [[ //~ config ] //~ block ]
inlineenv = beginenv { textelements }+ endenv
beginenv = "\begin{" §NAME §"}"
endenv = "\end{" §::NAME §"}"
footnote = "\footnote" block_of_paragraphs
#######################################################################
#
# low-level text and character sequences
#
#######################################################################
command = specialcmd | plaincmd
specialcmd = "\footnote" parblock
plaincmd = CMDNAME [[ //~ config ] //~ block ]
config = "[" cfgtext §"]"
block = /{/ { textelements } §/}/
......@@ -71,6 +108,13 @@ blockcmd = "\subsection" | "\section" | "\chapter" | "\subsubsection"
| "\paragraph" | "\subparagraph" | "\begin{enumerate}"
| "\begin{itemize}" | "\item" | "\begin{figure}"
#######################################################################
#
# Primitives
#
#######################################################################
CMDNAME = /\\(?:(?!_)\w)+/~
NAME = /\w+/~
......@@ -82,5 +126,4 @@ WSPC = /[ \t]+/ # (horizontal) whitespace
LF = !PARSEP /[ \t]*\n[ \t]*/ # LF but not an empty line
PARSEP = /[ \t]*(?:\n[ \t]*)+\n[ \t]*/ # at least one empty line, i.e.
# [whitespace] linefeed [whitespace] linefeed
EOF = !/./
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment