Skip to content

Commit

Permalink
OD-17115 Fix of DslDoc generation task
Browse files Browse the repository at this point in the history
  • Loading branch information
KeyrisXdSnow committed Jul 10, 2024
1 parent 714a844 commit e9ecece
Show file tree
Hide file tree
Showing 8 changed files with 1,177 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package au.com.ish.docs
import groovyjarjarantlr.RecognitionException
import groovyjarjarantlr.TokenStreamException
import groovyjarjarantlr.collections.AST
import org.apache.groovy.antlr.override.GroovydocVisitor
import org.codehaus.groovy.antlr.AntlrASTProcessor
import org.codehaus.groovy.antlr.SourceBuffer
import org.codehaus.groovy.antlr.UnicodeEscapingReader
Expand All @@ -28,13 +29,13 @@ import org.codehaus.groovy.antlr.java.JavaRecognizer
import org.codehaus.groovy.antlr.parser.GroovyLexer
import org.codehaus.groovy.antlr.parser.GroovyRecognizer
import org.codehaus.groovy.antlr.treewalker.PreOrderTraversal
import org.codehaus.groovy.antlr.treewalker.SourceCodeTraversal
import org.codehaus.groovy.antlr.treewalker.Visitor
import org.codehaus.groovy.ast.ModuleNode
import org.codehaus.groovy.control.*
import org.codehaus.groovy.groovydoc.GroovyClassDoc
import org.codehaus.groovy.groovydoc.GroovyRootDoc
import org.codehaus.groovy.runtime.ResourceGroovyMethods
import org.codehaus.groovy.tools.groovydoc.LinkArgument
import org.codehaus.groovy.tools.groovydoc.SimpleGroovyClassDocAssembler
import org.codehaus.groovy.tools.groovydoc.SimpleGroovyExecutableMemberDoc
import org.codehaus.groovy.tools.groovydoc.SimpleGroovyPackageDoc
import org.codehaus.groovy.tools.groovydoc.SimpleGroovyRootDoc
Expand Down Expand Up @@ -74,11 +75,11 @@ class DslGroovyRootDocBuilder {
unicodeReader.setLexer(lexer)
parser = GroovyRecognizer.make(lexer)
}
parser.setSourceBuffer(sourceBuffer)
parser.compilationUnit()
AST ast = parser.getAST()

if (isJava) {
parser.setSourceBuffer(sourceBuffer)
parser.compilationUnit()
AST ast = parser.getAST()

// modify the Java AST into a Groovy AST (just token types)
Visitor java2groovyConverter = new Java2GroovyConverter(parser.getTokenNames())
AntlrASTProcessor java2groovyTraverser = new PreOrderTraversal(java2groovyConverter)
Expand All @@ -89,10 +90,36 @@ class DslGroovyRootDocBuilder {
AntlrASTProcessor groovifierTraverser = new PreOrderTraversal(groovifier)
groovifierTraverser.process(ast)
}
Visitor visitor = new SimpleGroovyClassDocAssembler(packagePath, file, sourceBuffer, links, properties, !isJava)
AntlrASTProcessor traverser = new SourceCodeTraversal(visitor)
traverser.process(ast)
return ((SimpleGroovyClassDocAssembler)visitor).getGroovyClassDocs()

CompilerConfiguration config = new CompilerConfiguration()
config.getOptimizationOptions().put(CompilerConfiguration.GROOVYDOC, true)
CompilationUnit compUnit = new CompilationUnit(config)
SourceUnit unit = new SourceUnit(file, src, config, null, new ErrorCollector(config));
compUnit.addSource(unit);
int phase = Phases.CONVERSION;
if (properties.containsKey("phaseOverride")) {
String raw = properties.getProperty("phaseOverride")
try {
phase = Integer.parseInt(raw)
} catch(NumberFormatException ignore) {
raw = raw.toUpperCase();
switch(raw) {
// some dup here but kept simple since we may swap Phases to an enum
case "CONVERSION": phase = 3; break;
case "SEMANTIC_ANALYSIS": phase = 4; break;
case "CANONICALIZATION": phase = 5; break;
case "INSTRUCTION_SELECTION": phase = 6; break;
case "CLASS_GENERATION": phase = 7; break;
default:
System.err.println("Ignoring unrecognised or unsuitable phase and keeping default");
}
}
}
compUnit.compile(phase);
ModuleNode root = unit.getAST()
GroovydocVisitor visitor = new GroovydocVisitor(unit, packagePath, links, properties)
root.getClasses().forEach(clazz -> visitor.visitClass(clazz))
return visitor.getGroovyClassDocs()
}

protected void setOverview() {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
/*
* Copyright ish group pty ltd 2024.
*
* This program is free software: you can redistribute it and/or modify it under the terms of the GNU Affero General Public License version 3 as published by the Free Software Foundation.
*
* 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 Affero General Public License for more details.
*/

/*
* 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.
*/

package org.apache.groovy.antlr.override;

import org.codehaus.groovy.runtime.DefaultGroovyMethods;
import org.codehaus.groovy.runtime.DefaultGroovyMethodsSupport;
import org.codehaus.groovy.util.ArrayIterator;

/**
* Defines new groovy methods which appear on arrays inside the Groovy environment.
* Static methods are used with the first parameter being the destination class,
* i.e. <code>public static int[] each(int[] self, Closure closure)</code>
* provides an <code>each({i -> })</code> method for <code>int[]</code>.
* <p>
* NOTE: While this class contains many 'public' static methods, it is
* primarily regarded as an internal class (its internal package name
* suggests this also). We value backwards compatibility of these
* methods when used within Groovy but value less backwards compatibility
* at the Java method call level. I.e. future versions of Groovy may
* remove or move a method call in this file but would normally
* aim to keep the method available from within Groovy.
*/

// TODO: Remove after update Groovy v4, this part of code is taken from this version

public class ArrayGroovyMethods extends DefaultGroovyMethodsSupport {

private ArrayGroovyMethods() {
}

/**
* Concatenates the string representation of each item in this array,
* with the given String as a separator between each item.
*
* <pre class="groovyTestCase">
* Serializable[] array = [1,2L,-3G]
* assert array.join("+") == "1+2+-3"
* </pre>
*
* @param self an array of Object
* @param separator a String separator
* @return the joined String
* @since 1.0
*/
public static String join(Object[] self, String separator) {
return DefaultGroovyMethods.join(new ArrayIterator<>(self), separator);
}
}
Loading

0 comments on commit e9ecece

Please sign in to comment.