File: emil_grammar.py

package info (click to toggle)
openmolcas 25.02-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 170,204 kB
  • sloc: f90: 498,088; fortran: 139,779; python: 13,587; ansic: 5,745; sh: 745; javascript: 660; pascal: 460; perl: 325; makefile: 17
file content (109 lines) | stat: -rw-r--r-- 4,329 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# -*- coding: utf-8 -*-

#***********************************************************************
# This file is part of OpenMolcas.                                     *
#                                                                      *
# OpenMolcas is free software; you can redistribute it and/or modify   *
# it under the terms of the GNU Lesser General Public License, v. 2.1. *
# OpenMolcas is distributed in the hope that it will be useful, but it *
# is provided "as is" and without any express or implied warranties.   *
# For more details see the full text of the license in the file        *
# LICENSE or in <http://www.gnu.org/licenses/>.                        *
#                                                                      *
# Copyright (C) 2015-2017, Ignacio Fdez. Galván                        *
#***********************************************************************

from __future__ import (unicode_literals, division, absolute_import, print_function)

try:
  u = unicode
  del u
  py2 = True
except NameError:
  pass

from re import sub
from pyparsing import *

# helpful methods (python 2/3 compatibility)
def chomp(s):
  return s[:-1] if s.endswith('\n') else s

def chompAction(s, l, t):
  try:
    if (py2):
      pass
    return list(map(lambda s: chomp(unicode(s)), t))
  except NameError:
    return list(map(chomp, t))

def removeEMILEnd(s):
  return sub(r'\s*<*\s*$','',s)

def removeEMILEndAction(s, l, t):
  try:
    if (py2):
      pass
    return list(map(lambda s: removeEMILEnd(unicode(s)), t))
  except NameError:
    return list(map(removeEMILEnd, t))

################################################################################
# Attempt to specify a grammar for EMIL (extended Molcas input language)
################################################################################

# do not skip newlines when parsing
ParserElement.setDefaultWhitespaceChars(' \t')

# define a class that grabs the rest of the line, dropping the newline character
NL = Suppress( LineEnd() )
restOfLineNL = restOfLine + NL

# empty lines (between programs/commands)
EmptyLines = OneOrMore( NL )

# the same, but removing the optional "<" and spaces at the end of the line
restOfEMILLine = restOfLine.setParseAction(removeEMILEndAction)
restOfEMILLineNL = restOfEMILLine + NL

# and another that stops at a semicolon
RestComment = Suppress( Literal('//') + restOfLineNL )
restOfLineSemiColon = SkipTo( ( NL | RestComment ) | Suppress(Literal(';')), include=True )

# this seems to catch lines starting with spaces more reliably
#LineStartWS = Suppress( LineStart().leaveWhitespace() )
LineStartWS = Suppress( ZeroOrMore(White(' ')) )

# comments (line and block, and trailing newlines) are removed
LineComment = ( Literal('*') | Literal('//') ) + restOfLineNL
BlockComment = Literal('/*') + SkipTo( StringEnd() | Literal('*/'), include=True ) + Optional(NL)
TrailingNL = OneOrMore( NL ) + StringEnd()
Comment = Suppress( TrailingNL | BlockComment | LineComment )

# convenience alias
AnyLine = restOfLineSemiColon

# one-line EMIL commands start with one or more '>' and extend to the end of the line
EMILMark = (OneOrMore('>') + Optional(White())).setParseAction(replaceWith('>'))
EMILCommand = LineStartWS + Group(EMILMark + restOfEMILLineNL)

# the FILE command requires a closing line
EMILEOF = EMILMark + CaselessLiteral('EOF') + restOfEMILLineNL
EMILFileMark = ( originalTextFor( CaselessLiteral('FILE') + restOfLine ) + NL ).setParseAction(removeEMILEndAction)
EMILFile = LineStartWS + Group(EMILMark + EMILFileMark + SkipTo(EMILEOF).leaveWhitespace().setParseAction(chompAction)) + Suppress(EMILEOF)

# at the moment only FILE is a block command
EMILBlock = EMILFile

EMIL = EMILBlock | EMILCommand

# modules start with their name (preceded with '&') and contain every line
# until another module starts or an EMIL command is found
ModuleName = LineStartWS + Literal('&') + Word( alphanums + '_', min=2 ) + Suppress(restOfLineSemiColon)
ModuleEnd = StringEnd() | EMIL | ModuleName
Module = Group( ModuleName + ZeroOrMore( ~ModuleEnd + ( Comment | AnyLine ) ) )

# the input file should contain only modules, commands and comments
EMIL_Grammar = ZeroOrMore( EMIL | Module | Comment | EmptyLines )

################################################################################