All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.disney.groovity.compile.GroovitySourceTransformer Maven / Gradle / Ivy

There is a newer version: 2.1.0-beta.1
Show newest version
/*******************************************************************************
 * © 2018 Disney | ABC Television Group
 *
 * Licensed under the Apache License, Version 2.0 (the "Apache License")
 * with the following modification; you may not use this file except in
 * compliance with the Apache License and the following modification to it:
 * Section 6. Trademarks. is deleted and replaced with:
 *
 * 6. Trademarks. This License does not grant permission to use the trade
 *     names, trademarks, service marks, or product names of the Licensor
 *     and its affiliates, except as required to comply with Section 4(c) of
 *     the License and to reproduce the content of the NOTICE file.
 *
 * You may obtain a copy of the Apache License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the Apache License with the above modification is
 * distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied. See the Apache License for the specific
 * language governing permissions and limitations under the Apache License.
 *******************************************************************************/
package com.disney.groovity.compile;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;
import java.util.Stack;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * Translate Groovity source code into Groovy source code for compilation
 * 
 * @author Alex Vigdor
 */
public class GroovitySourceTransformer {
	static Pattern tagPattern = Pattern.compile("");
	static Pattern tagAttsPattern = Pattern.compile("(\\w+)\\s*=\\s*(?:\"\\$\\{([^\"]+?)}\"|\"(\\[[^\"]*\\])\"|(\"[^\"]*?\"))");
	static Pattern expressionStart = Pattern.compile("(? sourceLineNumbers = new HashMap();
		int transformedSourceLineNumber =1;
		int sourceLineNumber = 1;
		Scanner scanner = new Scanner(str);
		try{
		scanner.useDelimiter("((?=<~)|(?<=[^\\\\]~>))");
		while(scanner.hasNext()){
			String segment = scanner.next();
			//System.out.println("<-----------");
			//System.out.println(segment);
			//System.out.println(">-----------");
			if(segment.startsWith("<~")){
				segment = segment.substring(2, segment.length()-2);
				//we'll be making a writable closure
				output.append("new ClosureWritable({ ");
				Matcher tagMatcher = tagPattern.matcher(segment);
				int lastPos = 0;
				ArrayList subsegments = new ArrayList();
				if(tagMatcher.find()){
					tagMatcher.reset();
					while(tagMatcher.find()){
						if(tagMatcher.start()>lastPos){
							subsegments.add(segment.substring(lastPos,tagMatcher.start()));
						}
						subsegments.add(tagMatcher.group(0));
						lastPos = tagMatcher.end();
					}
					if(lastPos < segment.length()){
						subsegments.add(segment.substring(lastPos));
					}
					/*for(String sub:subsegments){
					System.out.println("<=-=-=-=-=-=-=-=-=-");
						System.out.println(sub);
						System.out.println(">=-=-=-=-=-=-=-=-=-");
						
					}*/
				}
				else{
					subsegments.add(segment);
				}
				
				for(String sub: subsegments){
					int prelines = 0;
					int postlines = 0;
					Matcher matcher;
					if(sub.startsWith("")){
							output.append(",{});\n");
							postlines++;
						}
						else{
							output.append(",{");
						}
					}
					else if(sub.startsWith("=0){
							if(exprPos>startFrom){
								String subSegment = escape(sub.substring(startFrom,exprPos));
								output.append("\nstream('''");
								if(subSegment.endsWith("'")){
									output.append(subSegment.substring(0,subSegment.length()-1));
									output.append("\\'");
								}	
								else{
									output.append(subSegment);
								}
								output.append("''');\n");
								postlines++;
								prelines++;
							}
							startFrom=exprPos+2;
							Stack stack = new Stack();
							//now the tricky part, find the right ending bracket .... 
							for (startFrom = exprPos+2;startFrom= startFrom)){
								if(!matcher.find()){
									exprPos = -1;
									break;
								}
								exprPos = matcher.start();
							}
						}
						if(startFrom < sub.length()){
							String subSegment = escape(sub.substring(startFrom));
							output.append("\nstream('''");
							if(subSegment.endsWith("'")){
								output.append(subSegment.substring(0,subSegment.length()-1));
								output.append("\\'");
							}	
							else{
								output.append(subSegment);
							}
							output.append("''');\n");
							postlines++;
							prelines++;
						}
					}
					else if(sub.trim().length() > 0){
						String subSegment = escape(sub);
						output.append("\nstream('''");
						if(subSegment.endsWith("'")){
							output.append(subSegment.substring(0,subSegment.length()-1));
							output.append("\\'");
						}	
						else{
							output.append(subSegment);
						}
						output.append("''');\n");
						postlines++;
						prelines++;
					}
					else{
						//just whitespace
						output.append(sub);
					}
					
					int subLines=countNewLines(sub);
					//System.out.println(subLines+" newlines in "+sub);
					for(int i=0;i",Matcher.quoteReplacement("~>")).replaceAll("\\\\",Matcher.quoteReplacement("\\\\"));
	}
	public static int countNewLines(String str) {
	    int lines = 0;
	    int pos = 0;
	    while ((pos = str.indexOf("\n", pos) + 1) != 0) {
	        lines++;
	    }
	    return lines;
	}
	
	public static class TransformedSource{
		public String originalSource;
		public String source;
		public Map sourceLineNumbers;
		public TransformedSource(String originalSource, String source, Map sourceLineNumbers){
			this.originalSource=originalSource;
			this.source=source;
			this.sourceLineNumbers=sourceLineNumbers;
		}
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy