Source

pida-poelzi-antlr / antlr / antlr.py

Full commit
# -*- coding: utf-8 -*- 

# Copyright (c) 2008 The PIDA Project

#Permission is hereby granted, free of charge, to any person obtaining a copy
#of this software and associated documentation files (the "Software"), to deal
#in the Software without restriction, including without limitation the rights
#to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
#copies of the Software, and to permit persons to whom the Software is
#furnished to do so, subject to the following conditions:

#The above copyright notice and this permission notice shall be included in
#all copies or substantial portions of the Software.

#THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
#IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
#FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
#AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
#LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
#OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
#SOFTWARE.

# stdlib

import sys
import os

# adjust the import path as antrl3 imports itself absolut :(
P = os.path.dirname(os.path.abspath(__file__))
if P not in sys.path:
    sys.path.append(P)

# core
from pida.core.service import Service
from pida.core.events import EventsConfig
from pida.core.actions import ActionsConfig, TYPE_NORMAL, TYPE_TOGGLE
from pida.core.options import OptionsConfig
from pida.core.features import FeaturesConfig

from pida.core.languages import Validator, Outliner

import antlr3

import _grammars

class AntlOutliner(Outliner):
    def parse(self):
        print self.document
        


class AntlrFeatures(FeaturesConfig):

    def subscribe_all_foreign(self):
        self.subscribe_foreign('filemanager', 'file_hidden_check',
            self.python)

class AntlrOptionsConfig(OptionsConfig):

    def create_options(self):
        self.create_option(
            'python_for_executing',
            _('Python Executable for executing'),
            str,
            'python',
            _('The Python executable when executing a module'),
        )


class AntlrEventsConfig(EventsConfig):

    def subscribe_all_foreign(self):
        self.subscribe_foreign('buffer', 'document-changed', self.on_document_changed)
        self.subscribe_foreign('buffer', 'document-saved', self.on_document_changed)

    def on_document_changed(self, document):
        pass

class AntlrActionsConfig(ActionsConfig):
    
    def create_actions(self):
        self.create_action(
            'execute_python',
            TYPE_NORMAL,
            _('Execute Python Module'),
            _('Execute the current Python module in a shell'),
            gtk.STOCK_EXECUTE,
            self.on_python_execute,
        )

        self.create_action(
            'show_python_errors',
            TYPE_TOGGLE,
            _('Python Error Viewer'),
            _('Show the python error browser'),
            'error',
            self.on_show_errors,
        )

        self.create_action(
            'show_python_source',
            TYPE_TOGGLE,
            _('Python Source Viewer'),
            _('Show the python source browser'),
            'info',
            self.on_show_source,
        )

    def on_python_execute(self, action):
        self.svc.execute_current_document()

    def on_show_errors(self, action):
        if action.get_active():
            self.svc.show_errors()
        else:
            self.svc.hide_errors()

    def on_show_source(self, action):
        if action.get_active():
            self.svc.show_source()
        else:
            self.svc.hide_source()


# Service class
class Antlr(Service):
    """Service for all things Python""" 
    
    #events_config = PythonEventsConfig
    #actions_config = PythonActionsConfig
    #options_config = PythonOptionsConfig
    #features_config = PythonFeatures

    def pre_start(self):
        """Start the service"""
        mgr = self.boss.get_service('language').doctypes
        import _grammars
        # add new types if not already added
        mgr.add_map(_grammars.TYPES)


# Required Service attribute for service loading
Service = Antlr



# vim:set shiftwidth=4 tabstop=4 expandtab textwidth=79: