Discussion:
preserving whitespace
Enrique Pérez
2012-10-26 14:48:15 UTC
Permalink
I am writing a parser for a grammar, and I want to allow Python sections
(delimitd by '\n<-\n' and '\n->\n') within code generated from that
grammar. I do not want to parse Python; I will feed it to exec. I just want
to get the whole lines, including whitespace.

This is in my lexer:

def t_begin_pycode(self, t):
r'<-'
t.lexer.begin('pycode')

def t_pycode_newline(self, t):
r'\n+'
t.lexer.lineno += len(t.value)

def t_pycode_IMPLIES(self, t):
r'->'
t.lexer.begin('INITIAL')
return t

def t_pycode_PYCODE(self, t):
r'.+'
return t

t_pycode_ignore = ''

However, somewhere between this and the parser, the leading whitespace is
lost...
What am I doing wrong?

I have looked at the GardenSnake parser by Andrew Dalke but I do not need
to parse indentation or distinguish types of whitespace.
--
You received this message because you are subscribed to the Google Groups "ply-hack" group.
To post to this group, send email to ply-***@googlegroups.com.
To unsubscribe from this group, send email to ply-hack+***@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msg/ply-hack/-/W2Y9RLtuVPMJ.
For more options, visit https://groups.google.com/groups/opt_out.
Enrique Pérez Arnaud
2012-10-30 17:51:58 UTC
Permalink
The problem was elsewhere, not in my usage of ply.

Sorry about the noise
Post by Enrique Pérez
I am writing a parser for a grammar, and I want to allow Python sections
(delimitd by '\n<-\n' and '\n->\n') within code generated from that
grammar. I do not want to parse Python; I will feed it to exec. I just want
to get the whole lines, including whitespace.
r'<-'
t.lexer.begin('pycode')
r'\n+'
t.lexer.lineno += len(t.value)
r'->'
t.lexer.begin('INITIAL')
return t
r'.+'
return t
t_pycode_ignore = ''
However, somewhere between this and the parser, the leading whitespace is
lost...
What am I doing wrong?
I have looked at the GardenSnake parser by Andrew Dalke but I do not need
to parse indentation or distinguish types of whitespace.
--
You received this message because you are subscribed to the Google Groups "ply-hack" group.
To unsubscribe from this group, send email to
To view this discussion on the web visit
https://groups.google.com/d/msg/ply-hack/-/W2Y9RLtuVPMJ.
For more options, visit https://groups.google.com/groups/opt_out.
--
Enrique Pérez Arnaud
***@gmail.com
--
You received this message because you are subscribed to the Google Groups "ply-hack" group.
To post to this group, send email to ply-***@googlegroups.com.
To unsubscribe from this group, send email to ply-hack+***@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
Loading...