paul@101 | 1 | #!/usr/bin/env python |
paul@101 | 2 | |
paul@101 | 3 | """ |
paul@101 | 4 | Graphviz region metadata parser. This only identifies metadata, with the actual |
paul@101 | 5 | graph data being interpreted by Graphviz itself. |
paul@101 | 6 | |
paul@101 | 7 | Copyright (C) 2018 Paul Boddie <paul@boddie.org.uk> |
paul@101 | 8 | |
paul@101 | 9 | This program is free software; you can redistribute it and/or modify it under |
paul@101 | 10 | the terms of the GNU General Public License as published by the Free Software |
paul@101 | 11 | Foundation; either version 3 of the License, or (at your option) any later |
paul@101 | 12 | version. |
paul@101 | 13 | |
paul@101 | 14 | This program is distributed in the hope that it will be useful, but WITHOUT |
paul@101 | 15 | ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
paul@101 | 16 | FOR A PARTICULAR PURPOSE. See the GNU General Public License for more |
paul@101 | 17 | details. |
paul@101 | 18 | |
paul@101 | 19 | You should have received a copy of the GNU General Public License along with |
paul@101 | 20 | this program. If not, see <http://www.gnu.org/licenses/>. |
paul@101 | 21 | """ |
paul@101 | 22 | |
paul@185 | 23 | from moinformat.parsers.common import ParserBase, choice, get_patterns, group, \ |
paul@185 | 24 | optional |
paul@101 | 25 | from moinformat.parsers.moin import MoinParser |
paul@101 | 26 | from moinformat.tree.graphviz import Directive |
paul@101 | 27 | from moinformat.tree.moin import Text |
paul@101 | 28 | |
paul@101 | 29 | join = "".join |
paul@101 | 30 | |
paul@101 | 31 | # Parser functionality. |
paul@101 | 32 | |
paul@101 | 33 | class GraphvizParser(ParserBase): |
paul@101 | 34 | |
paul@101 | 35 | "A parser for Graphviz content, identifying format directives." |
paul@101 | 36 | |
paul@301 | 37 | formats = ["graphviz", "dot"] |
paul@109 | 38 | |
paul@101 | 39 | # Parser handler methods. |
paul@101 | 40 | |
paul@101 | 41 | def parse_directive(self, region): |
paul@101 | 42 | |
paul@101 | 43 | "Handle format directives." |
paul@101 | 44 | |
paul@185 | 45 | directive = self.match_group("directive") |
paul@188 | 46 | key = self.match_group("key") |
paul@188 | 47 | value = self.match_group("value") |
paul@185 | 48 | |
paul@188 | 49 | self.add_node(region, Directive(key, value, directive)) |
paul@101 | 50 | self.new_block(region) |
paul@101 | 51 | |
paul@101 | 52 | |
paul@101 | 53 | |
paul@101 | 54 | # Regular expressions. |
paul@101 | 55 | |
paul@101 | 56 | syntax = { |
paul@101 | 57 | # At start of line: |
paul@101 | 58 | |
paul@185 | 59 | "directive" : choice((join((r"^#", # # |
paul@185 | 60 | group("directive", r".*?$"), # rest of line |
paul@185 | 61 | optional(group("extra", r"\n")))), # nl (optional) |
paul@185 | 62 | |
paul@185 | 63 | # Legacy GraphvizParser directive syntax: |
paul@185 | 64 | |
paul@185 | 65 | join(("^//", # // |
paul@185 | 66 | group("key", ".*?"), # text-excl-eq-nl |
paul@185 | 67 | optional(join(("=", # eq (optional) |
paul@185 | 68 | group("value", ".*?")))), # text-excl-nl (optional) |
paul@185 | 69 | "\n")))), # nl |
paul@101 | 70 | |
paul@101 | 71 | "regionend" : MoinParser.syntax["regionend"], |
paul@101 | 72 | } |
paul@101 | 73 | |
paul@101 | 74 | patterns = get_patterns(syntax) |
paul@101 | 75 | |
paul@101 | 76 | |
paul@101 | 77 | |
paul@101 | 78 | # Pattern details. |
paul@101 | 79 | |
paul@101 | 80 | region_pattern_names = ["directive", "regionend"] |
paul@101 | 81 | |
paul@101 | 82 | |
paul@101 | 83 | |
paul@101 | 84 | # Pattern handlers. |
paul@101 | 85 | |
paul@101 | 86 | parse_region_end = ParserBase.parse_region_end |
paul@101 | 87 | |
paul@101 | 88 | handlers = { |
paul@101 | 89 | "directive" : parse_directive, |
paul@101 | 90 | "regionend" : parse_region_end, |
paul@101 | 91 | } |
paul@101 | 92 | |
paul@101 | 93 | parser = GraphvizParser |
paul@101 | 94 | |
paul@101 | 95 | # vim: tabstop=4 expandtab shiftwidth=4 |