cogre / wisent-dot.wy

;;; wisent-dot.wy --- GraphViz DOT file parser

;; Copyright (C) 2003, 2004 Eric M. Ludlam

;; Author: Eric Ludlam <zappo@gnu.org>
;; Keywords: syntax
;; X-RCS: $Id$

;; This file is not part of GNU Emacs.

;; This program is free software; you can redistribute it and/or
;; modify it under the terms of the GNU General Public License as
;; published by the Free Software Foundation; either version 2, or (at
;; your option) any later version.

;; This program is distributed in the hope that it will be useful, but
;; WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
;; General Public License for more details.

;; You should have received a copy of the GNU General Public License
;; along with this program; see the file COPYING.  If not, write to
;; the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
;; Boston, MA 02110-1301, USA.

;;; Commentary:
;;
;; Parser for GraphViz DOT language.
;; The language is declaritive and the whole thing is parsed.
;; The result could be used as a data structure representing a graph.

%languagemode  graphviz-dot-mode

%start dot_file
;; Needed for EXPANDFULL
%start graph-contents
%start node-description

;;; KEYWORDS
%type    <keyword>
%keyword DIGRAPH  "digraph"
%put     DIGRAPH  summary "digraph <name> { <graph elements> ... }"
%keyword GRAPH    "graph"
%put     GRAPH    summary "graph <name> { <graph elements> ... }"
%keyword SUBGRAPH "subgraph"
%put     SUBGRAPH summary "subgraph <name> { <graph elements> ... }"
%keyword NODE     "node"
%put     NODE     summary "node [<attribute>...];"
%keyword SHAPE    "shape"
%put     SHAPE    summary "shape=<shape-type>"
%keyword LABEL    "label"
%put     LABEL    summary "label=\"string\""
%keyword COLOR    "color"
%put     COLOR    summary "color=<color-spec>"
%keyword STYLE    "style"
%put     STYLE    summary "style=<style-spec>"
%keyword LEN      "len"
%put     LEN      summary "len=<value>"
%keyword FONTNAME "fontname"
%put     FONTNAME summary "fontname=<font-spec>"
%keyword FONTSIZE "fontsize"
%put     FONTSIZE summary "fontsize=<font-size-number>"
%keyword WIDTH    "width"
%keyword HEIGHT   "height"
%keyword SPLINES  "splines"
%keyword OVERLAP  "overlap"

;;; Punctuation Types
%type  <punctuation> syntax "\\s.+"
%token <punctuation> DILINK "->"
%put   DILINK summary "<node> -> <node>;   Directed link"
%token <punctuation> LINK   "--"
%put   LINK   summary "<node> -- <node>;   Link"
%token <punctuation> EQUAL  "="
%token <punctuation> SEMI   ";"
%token <punctuation> COMMA  ","

;;; BLOCK types
%type  <block>
%token <block>       BRACKET_BLOCK "(LBRACKET RBRACKET)"
%token <block>       BRACE_BLOCK   "(LBRACE RBRACE)"
%token <block>       PAREN_BLOCK   "(LPAREN RPAREN)"
%token <open-paren>  LBRACE        "{"
%token <close-paren> RBRACE        "}"
%token <open-paren>  LBRACKET      "["
%token <close-paren> RBRACKET      "]"
%token <open-paren>  LPAREN        "("
%token <close-paren> RPAREN        ")"


;;; Bland default types
%type  <symbol>
%token <symbol> symbol

%type  <string>
%token <string> string

%type  <number>
%token <number> number

%%

dot_file
  : digraph
  | graph
  ;

digraph
  : DIGRAPH symbol BRACE_BLOCK
    (TAG $2 'digraph :members (EXPANDFULL $3 graph-contents))
  ;

graph
  : GRAPH symbol BRACE_BLOCK
    (TAG $2 'graph :members (EXPANDFULL $3 graph-contents))
  ;

graph-contents
  : LBRACE
    ()
  | RBRACE
    ()
  | label
  | style
  | graph-attributes
  | subgraph
  | node
  | named-node
  | links
  ;

label
  : LABEL EQUAL string SEMI
    (TAG $3 'label)
  ;

style
  : STYLE EQUAL symbol SEMI
    (TAG $3 'style)
  ;

subgraph
  : SUBGRAPH symbol BRACE_BLOCK
    (TAG $2 'graph :members (EXPANDFULL $3 graph-contents))
  ;

node
  : NODE BRACKET_BLOCK SEMI
    (TAG "NODE" 'generic-node :attributes (EXPANDFULL $2 node-description))
  ;

graph-attributes
  : GRAPH BRACKET_BLOCK SEMI
    (TAG "GRAPH" 'graph-attributes :attributes (EXPANDFULL $2 node-description))
  ;

named-node
  : symbol BRACKET_BLOCK SEMI
    (TAG $1 'node :attributes (EXPANDFULL $2 node-description))
  ;

node-description
  : LBRACKET 
    ()
  | RBRACKET
    ()
  | COMMA
    ()
  | SHAPE EQUAL symbol
    (TAG $1 'attribute :value $3)
  | LABEL EQUAL string
    (TAG $1 'attribute :value $3)
  | FONTNAME EQUAL string
    (TAG $1 'attribute :value $3)
  | FONTSIZE EQUAL number
    (TAG $1 'attribute :value $3)
 ;; This is a catch-all in case we miss some keyword.
  | symbol EQUAL symbol
    (TAG $1 'attribute :value $3)
  ;

links
  : symbol DILINK symbol opt-link-attributes opt-semi
    (TAG $1 'link :to $3 :attributes $4)
  | BRACE_BLOCK
 ;; Links in braces get properties.  Deal with this later.
  ;

opt-semi
  : SEMI
    ()
  | ;; Empty
  ;

opt-link-attributes
  : BRACKET_BLOCK
    (EXPANDFULL $1 node-description)
  | ;; Empty
  ;

%%

(define-lex wisent-dot-lexer
  "Lexical analyzer that handles DOT buffers.
It ignores whitespace, newlines and comments."
  semantic-lex-ignore-whitespace
  semantic-lex-ignore-newline
  semantic-lex-ignore-comments
  wisent-dot-wy--<keyword>-keyword-analyzer
  wisent-dot-wy--<symbol>-regexp-analyzer
  wisent-dot-wy--<block>-block-analyzer
  ;; ?? semantic-lex-close-paren
  wisent-dot-wy--<number>-regexp-analyzer
  wisent-dot-wy--<string>-sexp-analyzer
  wisent-dot-wy--<punctuation>-string-analyzer
  semantic-lex-default-action
  )

;;; wisent-dot.wy ends here
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.