Skip to content
Snippets Groups Projects
Select Git revision
  • 274eddf04408825a84e68b4d4678cfba13134c42
  • master default
  • anders.blomdell
  • typeref
  • pragma
  • compiler-refactoring
  • labcomm2013
  • v2014.1
  • v2014.0
  • v2013.0
10 results

NameAnalysis.jrag

Blame
  • Forked from Anders Blomdell / LabComm
    Source project has a limited visibility.
    config.py 2.58 KiB
    #!/usr/bin/python3
    
    from  pyparsing import Keyword, QuotedString, Suppress, Regex, OneOrMore, Group
    from  pyparsing import lineno
    
    def checkSubdirectory(s, l, t):
        if t[0].startswith('/'):
            raise Exception('Subdirectory expected at line %d" %s' %
                            (lineno(l, s), t[0]))
    def checkDirectory(s, l, t):
        if not t[0].startswith('/'):
            raise Exception('Directory expected at line %d" %s' %
                            (lineno(l, s), t[0]))
        
    _subdirectory_ = (
        Regex('[._a-zA-Z0-9/]+') |
        QuotedString('"') |
        QuotedString("'")
    ).setParseAction(checkSubdirectory)
    _directory_ = (
        Regex('[._a-zA-Z0-9/]+') |
        QuotedString('"') |
        QuotedString("'")
    ).setParseAction(checkDirectory)
    _host_and_directory_ = (
        Regex('[A-Za-z][A-Za-z0-9-.]*')('host') +
        Suppress(':') +
        _directory_('path')
    )
    _client_ = (
        Regex('@?[A-Za-z][A-Za-z0-9-.]') | QuotedString('"') | QuotedString("'")
    )
    _options_ = (
        Regex('[a-z][a-z_,]*') | QuotedString('"') | QuotedString("'")
    )
    _client_export_ = (
        _client_('host') + _options_('options')
    )
    _export_entry_ = (
        _subdirectory_('path') +
        Suppress('[') +
        Group(OneOrMore(Group(_client_export_)))('client') +
        Suppress(']')
    )
    _primary_ = (
        Suppress(Keyword('primary')) + 
        Suppress('{') +
        Suppress(Keyword('attributes'))  +
        ( QuotedString('"') | QuotedString("'") )('attributes') +
        Group(_host_and_directory_)('mount') +
        Suppress(Keyword('export')) +
        Suppress('{') +
        Group(OneOrMore(Group(_export_entry_)))('export') +
        Suppress('}') +
        Suppress('}') 
    )
    _backup_entry_ = (
         Group(_host_and_directory_)('mount') +
        Suppress('[') +
        Group(OneOrMore(_subdirectory_)).setResultsName('path') +
        Suppress(']')
    )
    _backup_ = (
        Suppress(Keyword('backup')) +
        Suppress('{') +
        Group(OneOrMore(Group(_backup_entry_)))('entry') +
        Suppress('}')
    )
    _secondary_ = (
        Suppress(Keyword('secondary')) +
        Suppress('{') +
        Group(OneOrMore(Group(_backup_)))('backup') +
        Suppress('}')
    )
    _config_ = (
        Group(_primary_)('primary') +
        Group(_secondary_)('secondary')
    )
    
    def parse(s):
        return _config_.parseString(s)
    
    if __name__ == '__main__':
        import sys
        from  pyparsing import ParseException
    
        config = open(sys.argv[1]).read()
        try:
            config1 = parse(config)
        except ParseException as e:
            lines = config.split('\n')
            print('\n'.join(lines[0:e.lineno]))
            print('%s^ %s' % (' ' * e.col, e))
            raise
            pass
        config2 = parse(config)
        print(config1.dump())
        print(config1.asList() == config2.asList())