From 8aa42e73690f32c2c6bfdb11ff1f91cd3c90b12b Mon Sep 17 00:00:00 2001
From: Oleg Broytman
Date: Sat, 9 Jul 2016 21:39:06 +0300
Subject: [PATCH] Use new grako-based parser
---
.gitignore | 2 --
search-tags.py | 71 +++++++++++++++++++++++++-------------------------
2 files changed, 36 insertions(+), 37 deletions(-)
diff --git a/.gitignore b/.gitignore
index 682fdd4..539da74 100644
--- a/.gitignore
+++ b/.gitignore
@@ -1,3 +1 @@
*.py[co]
-parser.out
-parsetab.py
diff --git a/search-tags.py b/search-tags.py
index e40f05c..33d141f 100755
--- a/search-tags.py
+++ b/search-tags.py
@@ -7,10 +7,9 @@ __copyright__ = "Copyright (C) 2014-2016 PhiloSoft Design"
__license__ = "GNU GPL"
import cgi, sys
-from ply.lex import LexError
+from grako.exceptions import FailedParse
from html.response import redirect, response
-from parser.parser import parser
form = cgi.FieldStorage()
if not form.has_key('q'):
@@ -21,41 +20,43 @@ if not form.has_key('q'):
else:
q = form['q'].value
try:
- tree = parser.parse(q)
- except LexError:
- tree = None
- if tree is None:
+ from parser.parser import TagsParser
+ from parser.build_ast import TagsSemantics
+ parser = TagsParser(parseinfo=False)
+ tree = parser.parse(q, semantics=TagsSemantics())
+ except FailedParse:
status = "400 Bad request"
title = "Error!"
body = "Bad query syntax!"
- elif tree[0] == 'NAME': # Single tag - just do redirect
- tag = tree[1]
- assert isinstance(tag, str)
- from tags import tag_exists
- if tag_exists(tag):
- redirect("/Russian/blog/tags/%s.html" % tag, status="301 Moved")
- sys.exit()
- status = "404 Tag not found"
- title = "ïÛÉÂËÁ!"
- body = "ôÅÇ %s ÎÅ ÓÕÝÅÓÔ×ÕÅÔ!" % tag
- else: # Process tree
- from tags import find_tags
- posts = find_tags(tree)
- status = None
- title = "úÁÐÉÓÉ, ÎÁÊÄÅÎÎÙÅ ÄÌÑ ×ÙÒÁÖÅÎÉÑ " + cgi.escape(q)
- if posts:
- _posts = ["""\
-
-
-"""]
- for year, month, day, suburl, _title in posts:
- _posts.append('- %s-%s-%s %s
\n' % (year, month, day, suburl, _title))
- _posts .append("""\
-
-
-""")
- body = ''.join(_posts)
- else:
- body = "îÅ ÎÁÊÄÅÎÏ ÎÉ ÏÄÎÏÊ ÚÁÐÉÓÉ."
+ else:
+ if tree[0] == 'NAME': # Single tag - just do redirect
+ tag = tree[1]
+ assert isinstance(tag, str)
+ from tags import tag_exists
+ if tag_exists(tag):
+ redirect("/Russian/blog/tags/%s.html" % tag, status="301 Moved")
+ sys.exit()
+ status = "404 Tag not found"
+ title = "ïÛÉÂËÁ!"
+ body = "ôÅÇ %s ÎÅ ÓÕÝÅÓÔ×ÕÅÔ!" % tag
+ else: # Process tree
+ from tags import find_tags
+ posts = find_tags(tree)
+ status = None
+ title = "úÁÐÉÓÉ, ÎÁÊÄÅÎÎÙÅ ÄÌÑ ×ÙÒÁÖÅÎÉÑ " + cgi.escape(q)
+ if posts:
+ _posts = ["""\
+
+
+ """]
+ for year, month, day, suburl, _title in posts:
+ _posts.append('- %s-%s-%s %s
\n' % (year, month, day, suburl, _title))
+ _posts .append("""\
+
+
+ """)
+ body = ''.join(_posts)
+ else:
+ body = "îÅ ÎÁÊÄÅÎÏ ÎÉ ÏÄÎÏÊ ÚÁÐÉÓÉ."
response(title, body, status)
--
2.39.2