| /* |
| * 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. |
| */ |
| |
| options { |
| // Generate non-static functions |
| STATIC = false; |
| // Case is ignored in keywords |
| IGNORE_CASE = true; |
| } |
| |
| PARSER_BEGIN(DOTParser) |
| |
| package org.apache.pig.test.utils.dotGraph.parser ; |
| |
| import java.util.*; |
| import java.io.*; |
| import org.apache.pig.test.utils.dotGraph.* ; |
| |
| public class DOTParser { |
| |
| static String unquote(String s) { |
| return s.substring(1, s.length()-1); |
| } |
| |
| static class DotState { |
| public Map<String,String> nodeAttributes = new HashMap<String,String>() ; |
| public Map<String,String> edgeAttributes = new HashMap<String,String>() ; |
| } |
| |
| } |
| |
| PARSER_END(DOTParser) |
| |
| // Skip all the new lines, tabs and spaces |
| SKIP : { " " | "\r" | "\t" | "\n" } |
| |
| MORE : |
| { |
| "//" : SINGLE_COMMENT |
| | |
| "#" : SINGLE_COMMENT |
| | |
| "/*" : MULTI_COMMENT |
| } |
| |
| <MULTI_COMMENT> SPECIAL_TOKEN : |
| { |
| <("\n" | "\r" | "\r\n")> |
| | |
| <"*/"> : DEFAULT |
| } |
| |
| <SINGLE_COMMENT> SPECIAL_TOKEN : |
| { |
| < ("\n" | "\r" | "\r\n") > : DEFAULT |
| } |
| |
| <MULTI_COMMENT> MORE : |
| { |
| < ~[] > |
| } |
| |
| <SINGLE_COMMENT> MORE : |
| { |
| < ~[] > |
| } |
| |
| |
| TOKEN: |
| { |
| |
| <LPAREN : "{"> |
| | <RPAREN : "}"> |
| | <LSQBRACKET: "["> |
| | <RSQBRACKET: "]"> |
| | <EQUAL: "="> |
| | <COMMA: ","> |
| | <SEMICOLON: ";"> |
| | <DIRECTED_EDGE: "->"> |
| | <EDGE: "edge"> |
| | <NODE: "node"> |
| | <GRAPH: "graph"> |
| | <DIGRAPH : "digraph"> |
| | <#LETTER : ["a"-"z", "A"-"Z"] > |
| | <#DIGIT : ["0"-"9"] > |
| | <#SPECIAL_CHAR : "_" | "$" > |
| | <NAME : <LETTER> ( <LETTER> | <DIGIT> | <SPECIAL_CHAR> )* > |
| | <QUOTEDSTRING : "\"" (~["\""])* "\""> |
| } |
| |
| |
| DotGraph Parse() : |
| { |
| DotGraph dotGraph = null ; |
| DotState dotState = new DotState() ; |
| Token graphName ; |
| } |
| { |
| ( |
| <DIGRAPH> |
| graphName = <NAME> { dotGraph = new DotGraph(graphName.image) ; } |
| <LPAREN> |
| |
| ( LOOKAHEAD(2) |
| EdgeStatement(dotGraph, dotState) |
| | NodeStatement(dotGraph, dotState) |
| | AttributeStatement(dotGraph, dotState) |
| )+ |
| |
| <RPAREN> |
| ) |
| { return dotGraph ; } |
| } |
| |
| void AttributeStatement(DotGraph dotGraph, DotState dotState) : |
| { |
| Map<String,String> attributes ; |
| } |
| { |
| ( |
| ( <EDGE> attributes = AttributeList() { dotState.edgeAttributes = attributes ; } ) |
| | ( <NODE> attributes = AttributeList() { dotState.nodeAttributes = attributes ; } ) |
| | ( <GRAPH> attributes = AttributeList() { dotGraph.attributes = attributes ; } ) |
| ) |
| <SEMICOLON> |
| } |
| |
| void NodeStatement(DotGraph dotGraph, DotState dotState) : |
| { |
| Token nodeName ; |
| DotNode node = new DotNode() ; |
| Map<String,String> attributes ; |
| } |
| { |
| nodeName = <NAME> { node.name = nodeName.image ; } |
| ( attributes = AttributeList() { |
| node.attributes = new HashMap<String,String>() ; |
| if (dotState != null) { |
| node.attributes.putAll(dotState.nodeAttributes) ; |
| } |
| node.attributes.putAll(attributes) ; |
| } |
| )? |
| <SEMICOLON> |
| { dotGraph.nodes.add(node) ; } |
| } |
| |
| void EdgeStatement(DotGraph dotGraph, DotState dotState) : |
| { |
| Token nodeName1 ; |
| Token nodeName2 ; |
| String startingNode ; |
| DotNode node = new DotNode() ; |
| Map<String,String> attributes ; |
| } |
| { |
| nodeName1 = <NAME> { startingNode = nodeName1.image ; } |
| ( |
| <DIRECTED_EDGE> |
| nodeName2 = <NAME> |
| { |
| DotEdge edge = new DotEdge() ; |
| edge.fromNode = startingNode ; |
| edge.toNode = nodeName2.image ; |
| |
| dotGraph.edges.add(edge) ; |
| |
| startingNode = nodeName2.image ; |
| } |
| )+ |
| <SEMICOLON> |
| |
| } |
| |
| Map<String,String> AttributeList() : |
| { |
| Map<String,String> attributes = new HashMap<String,String>() ; |
| String[] keyValuePair ; |
| } |
| { |
| ( |
| <LSQBRACKET> |
| (keyValuePair = Attribute() { attributes.put(keyValuePair[0], keyValuePair[1]) ; } ) |
| ( |
| <COMMA> |
| (keyValuePair = Attribute() { attributes.put(keyValuePair[0], keyValuePair[1]) ; } ) |
| )* |
| <RSQBRACKET> |
| ) |
| { return attributes ; } |
| } |
| |
| String[] Attribute() : |
| { |
| Token attName ; |
| Token value ; |
| String[] keyValuePair = new String[2] ; |
| } |
| { |
| ( |
| attName = <NAME> { keyValuePair[0] = attName.image ; } |
| <EQUAL> |
| value = <QUOTEDSTRING> { keyValuePair[1] = unquote(value.image) ; } |
| ) |
| { return keyValuePair ; } |
| } |