# -*- coding: utf-8 -*-
+# Copyright (C) 2007-2013 EDF R&D
+#
+# This library is free software; you can redistribute it and/or
+# modify it under the terms of the GNU Lesser General Public
+# License as published by the Free Software Foundation; either
+# version 2.1 of the License.
+#
+# This library 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
+# Lesser General Public License for more details.
+#
+# You should have received a copy of the GNU Lesser General Public
+# License along with this library; if not, write to the Free Software
+# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+#
+# See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
+#
import re,string
import compiler
allcharsExceptNewline = allchars[: allchars.index('\n')]+allchars[allchars.index('\n')+1:]
allcharsExceptNewlineTranstable = string.maketrans(allcharsExceptNewline, '*'*len(allcharsExceptNewline))
+#------------------------------
def maskStringsAndComments(src):
+#------------------------------
"""Remplace tous les caracteres dans commentaires et strings par des * """
+
src = escapedQuotesRE.sub("**", src)
allstrings = stringsAndCommentsRE.split(src)
# every odd element is a string or comment
linecontinueRE = re.compile(r"\\\s*(#.*)?$")
emptyHangingBraces = [0,0,0,0,0]
+#--------------------------------------
class UnbalancedBracesException: pass
+#--------------------------------------
+#-----------
class Node:
+#-----------
def __init__(self):
self.childNodes=[]
self.childNodes.append(node)
-class FactNode(Node):pass
+#-------------------
+class FactNode(Node):
+#-------------------
+ pass
+
+
+#-------------------
class JDCNode(Node):
+#-------------------
def __init__(self,src):
Node.__init__(self)
self.src=src
+#-------------------
class Command(Node):
+#-------------------
def __init__(self,name,lineno,colno,firstparen):
Node.__init__(self)
self.name=name
self.colno=colno
self.firstparen=firstparen
+#-------------------
class Keyword(Node):
+#-------------------
def __init__(self,name,lineno,colno,endline,endcol):
Node.__init__(self)
self.name=name
def getText(self,jdc):
if self.endline > self.lineno:
- lignecourante=self.lineno + 1
debut=jdc.getLines()[self.lineno-1][self.colno:]
fin = jdc.getLines()[self.endline-1][:self.endcol]
texte=debut
- lignecourante=self.lineno + 1
- while lignecourante > self.endline :
+ lignecourante=self.lineno
+ while lignecourante < self.endline -1 :
texte = texte + jdc.getLines()[lignecourante]
lignecourante = lignecourante + 1
if chaineBlanche(fin) == 0 :
texte = jdc.getLines()[self.lineno-1][self.colno:self.endcol]
return texte
+#-------------------------
def chaineBlanche(texte) :
+#-------------------------
# retourne 1 si la chaine est composee de " "
# retourne 0 sinon
bool = 1 ;
if texte[i] != " " : bool = 0
return bool
+#-------------------
def printNode(node):
+#-------------------
if hasattr(node,'name'):
print node.name
else:
for c in node.childNodes:
printNode(c)
-def parser(src,atraiter):
+#------------------------
+def Parser(src,atraiter):
+#------------------------
"""Parse le texte src et retourne un arbre syntaxique (root).
Cet arbre syntaxique a comme noeuds (childNodes) les commandes à traiter (liste atraiter)
root=JDCNode(src)
- # (a) dans un premier temps on extrait les commandes et on les insère dans un arbre (root)
- # les noeuds fils sont stockés dans root.childNodes (liste)
+ # (a) dans un premier temps on extrait les commandes et on les insère
+ # dans un arbre (root) les noeuds fils sont stockés dans
+ # root.childNodes (liste)
lineno=0
for line in maskedLines:
lineno=lineno+1
if m and (m.group(2) in atraiter):
root.addChild(Command(m.group(2),lineno,m.start(2),m.end(4)))
- #(b) dans un deuxième temps , on récupère le texte complet de la commande jusqu'à la
- # dernière parenthèse fermante
+ #(b) dans un deuxième temps , on récupère le texte complet de la commande
+ # jusqu'à la dernière parenthèse fermante
- #iterateur sur les lignes physiques masquées
+ # iterateur sur les lignes physiques masquées
iterlines=iter(maskedLines)
linenum=0
for c in root.childNodes:
lineno=c.lineno
- colno=c.colno # début de la commande
+ colno=c.colno # début de la commande
while linenum < lineno:
line=iterlines.next()
linenum=linenum+1
return root
+#-----------------
def lastparen(src):
+#-----------------
"""Retourne la position de la derniere parenthese fermante dans src a partir du debut de la string
La string doit contenir la premiere parenthese ouvrante
"""
+
src=maskStringsAndComments(src)
level=0
i,n=0,len(src)
#derniere parenthese fermante
return i
+#-------------------
def lastparen2(src):
+#-------------------
"""Retourne la position de la derniere parenthese fermante dans src a partir du debut de la string
La string ne contient pas la premiere parenthese ouvrante