From: Martin Puttke <martinstefanputtke@posteo.de>
To: 46817@debbugs.gnu.org
Subject: bug#46817: update python grammar
Date: Sat, 27 Feb 2021 19:04:53 +0100 [thread overview]
Message-ID: <87im6d5quy.fsf@posteo.de> (raw)
[-- Attachment #1: Type: text/plain, Size: 258 bytes --]
Hi out there,
I made some fixes to enhance the python grammar file to enable it
to understand python type hints and have non-trivial base classes.
Please feel free to include my changes into emacs.
please find my patches attached.
All the best
Martin
[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #2: 0001-add-python-grammar-for-type-hints-in-variable-defini.patch --]
[-- Type: text/x-diff, Size: 960 bytes --]
From b5eaf8174267ac466ad3d8be63bae4448f476d16 Mon Sep 17 00:00:00 2001
From: Kapuze Martin <m.s.p@posteo.de>
Date: Wed, 18 Nov 2020 17:54:22 +0100
Subject: [PATCH 1/3] add python grammar for type hints in variable definitions
---
admin/grammars/python.wy | 11 ++++++++++-
1 file changed, 10 insertions(+), 1 deletion(-)
diff --git a/admin/grammars/python.wy b/admin/grammars/python.wy
index aaa25ced20..b7254e2533 100644
--- a/admin/grammars/python.wy
+++ b/admin/grammars/python.wy
@@ -1140,7 +1140,7 @@ fpdef_opt_test
;; fpdef: NAME | '(' fplist ')'
fpdef
- : NAME
+ : NAME type_hint
(VARIABLE-TAG $1 nil nil)
;; Below breaks the parser. Don't know why, but my guess is that
;; LPAREN/RPAREN clashes with the ones in function_parameters.
@@ -1160,6 +1160,15 @@ fpdef
;; | fpdef_list COMMA fpdef
;; ;
+type_hint
+ : ;;EMPTY
+ | COLON type
+ ;
+
+type
+ : dotted_name
+ ;
+
;; ['=' test]
eq_test_opt
: ;;EMPTY
--
2.25.1
[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #3: 0002-add-python-grammar-for-type-hints-for-function-retur.patch --]
[-- Type: text/x-diff, Size: 1405 bytes --]
From 3d8c711acc63bb2e61dfeccb73223ff3bf29b67e Mon Sep 17 00:00:00 2001
From: Kapuze Martin <m.s.p@posteo.de>
Date: Wed, 18 Nov 2020 18:11:36 +0100
Subject: [PATCH 2/3] add python grammar for type hints for function return
value
---
admin/grammars/python.wy | 14 ++++++++++----
1 file changed, 10 insertions(+), 4 deletions(-)
diff --git a/admin/grammars/python.wy b/admin/grammars/python.wy
index b7254e2533..cc5093b0b7 100644
--- a/admin/grammars/python.wy
+++ b/admin/grammars/python.wy
@@ -184,6 +184,7 @@
%token <punctuation> ASSIGN "="
%token <punctuation> BACKQUOTE "`"
%token <punctuation> AT "@"
+%token <punctuation> FOLLOWS "->"
;; -----------------
@@ -808,12 +809,17 @@ decorators
;; funcdef: [decorators] 'def' NAME parameters ':' suite
funcdef
- : DEF NAME function_parameter_list COLON suite
+ : DEF NAME function_parameter_list return_type_hint COLON suite
(wisent-python-reconstitute-function-tag
- (FUNCTION-TAG $2 nil $3) $5)
- | decorators DEF NAME function_parameter_list COLON suite
+ (FUNCTION-TAG $2 nil $3) $6)
+ | decorators DEF NAME function_parameter_list return_type_hint COLON suite
(wisent-python-reconstitute-function-tag
- (FUNCTION-TAG $3 nil $4 :decorators $1) $6)
+ (FUNCTION-TAG $3 nil $4 :decorators $1) $7)
+ ;
+
+return_type_hint
+ : ;;EMPTY
+ | FOLLOWS type
;
function_parameter_list
--
2.25.1
[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #4: 0003-generalize-type-definition-and-apply-it-to-parent-cl.patch --]
[-- Type: text/x-diff, Size: 877 bytes --]
From cf4cd6d4ca1debc6f0b82aa5c9a03a78730980b1 Mon Sep 17 00:00:00 2001
From: Kapuze Martin <m.s.p@posteo.de>
Date: Mon, 23 Nov 2020 21:56:09 +0100
Subject: [PATCH 3/3] generalize type definition and apply it to parent classes
---
admin/grammars/python.wy | 4 ++--
1 file changed, 2 insertions(+), 2 deletions(-)
diff --git a/admin/grammars/python.wy b/admin/grammars/python.wy
index cc5093b0b7..bb3d96d703 100644
--- a/admin/grammars/python.wy
+++ b/admin/grammars/python.wy
@@ -893,7 +893,7 @@ paren_classes
;; parser can parse general expressions, I don't see much benefit in
;; generating a string of expression as base class "name".
paren_class
- : dotted_name
+ : type
;
;;;****************************************************************************
@@ -1172,7 +1172,7 @@ type_hint
;
type
- : dotted_name
+ : test
;
;; ['=' test]
--
2.25.1
[-- Attachment #5: Type: text/plain, Size: 49 bytes --]
--
Composed with Spacemacs (www.spacemacs.org)
next reply other threads:[~2021-02-27 18:04 UTC|newest]
Thread overview: 4+ messages / expand[flat|nested] mbox.gz Atom feed top
2021-02-27 18:04 Martin Puttke [this message]
2021-02-28 14:22 ` bug#46817: update python grammar Lars Ingebrigtsen
2021-03-02 18:08 ` Martin Puttke
2021-03-03 14:52 ` Lars Ingebrigtsen
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
List information: https://www.gnu.org/software/emacs/
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=87im6d5quy.fsf@posteo.de \
--to=martinstefanputtke@posteo.de \
--cc=46817@debbugs.gnu.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
Code repositories for project(s) associated with this public inbox
https://git.savannah.gnu.org/cgit/emacs.git
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).