alex / alex/rply

What is the equivalent of PLY's "def t_FOO(t): ..."?

オープン
#15 コメント 1 件 リアクション 0 件 担当者 0 名 GitHub で見る
主要言語
Python
スター
391
フォーク
62
PR マージ指標
30日以内にマージされた PR はありません

説明

In a PLY lexer, I can implement certain weird things such as case-insensitive keywords by defining a function with the same name as I'd normally give the string variable containing the regexp for that token.

For example:

```
from ply import lex

tokens = ("GREET", "FIGHT", "WORD")
reserved = ("GREET", "FIGHT")

t_ignore = ' +'

def t_error(t):
raise ValueError("oh noooo")

def t_WORD(t):
"[a-zA-Z]+"
upper = t.value.upper()
if upper in reserved:
t.value = upper
t.type = upper
return t

lexer = lex.lex()
lexer.input("grEEt samuel FIGHT tomato greet potato FIght pOEtRY")
for token in lexer:
print token

#LexToken(GREET,'GREET',1,0)
#LexToken(WORD,'samuel',1,6)
#LexToken(FIGHT,'FIGHT',1,13)
#LexToken(WORD,'tomato',1,19)
#LexToken(GREET,'GREET',1,26)
#LexToken(WORD,'potato',1,32)
#LexToken(FIGHT,'FIGHT',1,39)
#LexToken(WORD,'pOEtRY',1,45)
```

I can't find anything in rply's documentation that explains how to do the equivalent of defining t_WORD as a function in the above program. Nor can I find anything that indicates that it can't be done.

コントリビューションガイド

このリポジトリのコントリビューションガイドは索引されていません

評価

この issue はまだ評価されていません。

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。