1. cormullion
  2. pygments-main

Commits

Georg Brandl  committed 6ef790a Merge
  • Participants
  • Parent commits da099b2, f261690
  • Branches default

Comments (0)

Files changed (4)

File AUTHORS

View file
 * James H. Fisher -- PostScript lexer
 * Naveen Garg - Autohotkey lexer
 * Laurent Gautier -- R/S lexer
+* Bertrand Goetzmann -- Groovy lexer
 * Krzysiek Goj -- Scala lexer
 * Matt Good -- Genshi, Cheetah lexers
 * Patrick Gotthardt -- PHP namespaces support

File CHANGES

View file
   * Nimrod
   * Nemerle (#667)
   * F# (#353)
+  * Groovy (#501)
 
 - In the LaTeX formatter, escape special &, < and > chars (#648).
 

File pygments/lexers/agile.py

View file
 import re
 
 from pygments.lexer import Lexer, RegexLexer, ExtendedRegexLexer, \
-     LexerContext, include, combined, do_insertions, bygroups, using
+     LexerContext, include, combined, do_insertions, bygroups, using, this
 from pygments.token import Error, Text, Other, \
      Comment, Operator, Keyword, Name, String, Number, Generic, Punctuation
 from pygments.util import get_bool_opt, get_list_opt, shebang_matches
             (r'\d+', Number.Integer)
         ]
     }
+
+
+class GroovyLexer(RegexLexer):
+    """
+    For `Groovy <http://groovy.codehaus.org/>`_ source code.
+
+    *New in Pygments 1.5.*
+    """
+
+    name = 'Groovy'
+    aliases = ['groovy']
+    filenames = ['*.groovy']
+    mimetypes = ['text/x-groovy']
+
+    flags = re.MULTILINE | re.DOTALL
+
+    #: optional Comment or Whitespace
+    _ws = r'(?:\s|//.*?\n|/[*].*?[*]/)+'
+
+    tokens = {
+        'root': [
+            # method names
+            (r'^(\s*(?:[a-zA-Z_][a-zA-Z0-9_\.\[\]]*\s+)+?)' # return arguments
+             r'([a-zA-Z_][a-zA-Z0-9_]*)'                    # method name
+             r'(\s*)(\()',                                  # signature start
+             bygroups(using(this), Name.Function, Text, Operator)),
+            (r'[^\S\n]+', Text),
+            (r'//.*?\n', Comment.Single),
+            (r'/\*.*?\*/', Comment.Multiline),
+            (r'@[a-zA-Z_][a-zA-Z0-9_\.]*', Name.Decorator),
+            (r'(assert|break|case|catch|continue|default|do|else|finally|for|'
+             r'if|goto|instanceof|new|return|switch|this|throw|try|while|in|as)\b',
+             Keyword),
+            (r'(abstract|const|enum|extends|final|implements|native|private|'
+             r'protected|public|static|strictfp|super|synchronized|throws|'
+             r'transient|volatile)\b', Keyword.Declaration),
+            (r'(def|boolean|byte|char|double|float|int|long|short|void)\b',
+             Keyword.Type),
+            (r'(package)(\s+)', bygroups(Keyword.Namespace, Text)),
+            (r'(true|false|null)\b', Keyword.Constant),
+            (r'(class|interface)(\s+)', bygroups(Keyword.Declaration, Text), 'class'),
+            (r'(import)(\s+)', bygroups(Keyword.Namespace, Text), 'import'),
+            (r'"(\\\\|\\"|[^"])*"', String.Double),
+            (r"'(\\\\|\\'|[^'])*'", String.Single),
+            (r'\$/((?!/\$).)*/\$', String),
+            (r'/(\\\\|\\"|[^/])*/', String),
+            (r"'\\.'|'[^\\]'|'\\u[0-9a-f]{4}'", String.Char),
+            (r'(\.)([a-zA-Z_][a-zA-Z0-9_]*)', bygroups(Operator, Name.Attribute)),
+            (r'[a-zA-Z_][a-zA-Z0-9_]*:', Name.Label),
+            (r'[a-zA-Z_\$][a-zA-Z0-9_]*', Name),
+            (r'[~\^\*!%&\[\]\(\)\{\}<>\|+=:;,./?-]', Operator),
+            (r'[0-9][0-9]*\.[0-9]+([eE][0-9]+)?[fd]?', Number.Float),
+            (r'0x[0-9a-f]+', Number.Hex),
+            (r'[0-9]+L?', Number.Integer),
+            (r'\n', Text)
+        ],
+        'class': [
+            (r'[a-zA-Z_][a-zA-Z0-9_]*', Name.Class, '#pop')
+        ],
+        'import': [
+            (r'[a-zA-Z0-9_.]+\*?', Name.Namespace, '#pop')
+        ],
+    }

File tests/examplefiles/test.groovy

View file
+// This source code comes from http://www.odelia-technologies.com/node/200
+
+package com.odelia.groovy.simpleworkflow
+
+
+class SimpleWorkflowEngine {
+    def workflowMap = [:]
+    def context = [:]
+    def beforeActivityName = 'beforeActivity'
+    def afterActivityName = 'afterActivity'
+
+    SimpleWorkflowEngine(workflow, context = [:]) {
+        this.context = context
+        parseWorkflow(workflow)
+    }
+
+    def parseWorkflow(workflow) {
+        workflowMap = new WorkflowParser().parse(workflow)
+    }
+
+    def getActivityValue(activity) {
+        assert activity instanceof String
+        if (!workflowMap[activity])
+            throw new RuntimeException("$activity activity doesn't exist")
+        workflowMap[activity]
+    }
+
+    def execute(activity, pause) {
+        if (workflowMap[beforeActivityName]) {
+            getActivityValue(beforeActivityName)(context, activity)
+        }
+
+        def activityValue = getActivityValue(activity)
+
+        // Determine the next activity to execute
+        def nextActivity
+        switch (activityValue) {
+            case String: nextActivity = activityValue; break
+            case Closure: nextActivity = activityValue(context); break
+            case Class: nextActivity = activityValue.newInstance()(context)
+        }
+
+        if (workflowMap[afterActivityName]) {
+            getActivityValue(afterActivityName)(context, activity, nextActivity)
+        }
+
+        if (!pause && nextActivity)
+            call(nextActivity)
+        else
+            nextActivity
+    }
+
+    def call(activity) {
+        execute(activity, false)
+    }
+
+    def nextActivity(activity) {
+        execute(activity, true)
+    }
+
+    static void main(String[] args) {
+        if (args.size() != 2) {
+            println 'Usage: com.odelia.groovy.simpleworkflow.SimpleWorkflowEngine <dsl_filename> <activity_name>'
+            return
+        }
+        SimpleWorkflowEngine.newInstance(new File(args[0]))(args[1])
+    }
+
+}
+
+private class WorkflowParser {
+    def map = [:]
+
+    def methodMissing(String name, args) {
+        map[name] = args[0]
+    }
+
+    def parse(Closure wf) {
+        wf.delegate = this
+        wf.resolveStrategy = Closure.DELEGATE_FIRST
+        wf()
+        map
+    }
+
+    def workflow = { it ->
+        it.delegate = this
+        it.resolveStrategy = Closure.DELEGATE_FIRST
+        it()
+    }
+
+    def parse(File workflowDef) {
+        def binding = new Binding([workflow: workflow])
+        def shell = new GroovyShell(binding)
+        shell.evaluate(workflowDef)
+        map
+    }
+}