Juan Hernandez has uploaded a new change for review.

Change subject: cli: No regular expression for IP, UUID or number
......................................................................

cli: No regular expression for IP, UUID or number

Currently the command line lexer uses regular expressions to match IP
addresses, UUIDs and numbers. But in the parser grammar there is no
production that accepts those terminal symbols and doesn't accept also
plain words. The only special treatment is that numeric values are
converted to "int".

This behaviour means that some words that happen to start like IP
addresses, UUIDS or numbers are split into two tokens. For example, the
following command line:

  update host 10.10.10.10a --name 10.10.10.10

Will be split into the following tokens:

  WORD: update
  WORD: host
  IPADDR: 10.10.10.10
  WORD: a
  OPTION: --name
  IPADDR: 10.10.10.10

But it should have been as follows:

  WORD: update
  WORD: host
  WORD: 10.10.10.10a
  OPTION: --name
  IPADDR: 10.10.10.10

To avoid this issue, and simplify the lexer, this patch removes these
regular expressions for IP addresses, UUIDs and numbers, and replaces
them with a single expression that matches words. The action for this
expression will check if the value is actually an integer and convert it
to "int".

Change-Id: If670144751fdb9564380a12fb73596ecbaa47ee9
Bug-Url: https://bugzilla.redhat.com/1110366
Signed-off-by: Juan Hernandez <juan.hernan...@redhat.com>
---
M src/cli/parser.py
1 file changed, 17 insertions(+), 24 deletions(-)


  git pull ssh://gerrit.ovirt.org:29418/ovirt-engine-cli refs/changes/22/29722/1

diff --git a/src/cli/parser.py b/src/cli/parser.py
index 4158a99..70e9f27 100644
--- a/src/cli/parser.py
+++ b/src/cli/parser.py
@@ -31,14 +31,12 @@
     multi-line inputs.
     """
 
-    tokens = ('IPADDR', 'UUID', 'WORD', 'STRING', 'NUMBER', 'OPTION', 'LT', 
'LTLT', 'GT', 'GTGT',
+    tokens = ('WORD', 'STRING', 'OPTION', 'LT', 'LTLT', 'GT', 'GTGT',
               'BANG', 'PIPE', 'NEWLINE', 'MARKER', 'HEREDOC', 'SHELL')
     literals = ('=', ';')
     states = [('heredoc1', 'inclusive'), ('heredoc2', 'exclusive'),
               ('shell', 'exclusive')]
 
-    t_WORD = r'[^ \n\t"\'<>|!\\#;]+'
-    t_OPTION = r'-(-[a-zA-Z_][a-zA-Z0-9_]*)+'
     t_LT = r'<'
     t_GT = r'>'
     t_GTGT = r'>>'
@@ -49,16 +47,22 @@
     t_heredoc2_ignore = ' \t'
     t_shell_ignore = ' \t'
 
-    def t_IPADDR(self, t):
-        # ip validity check performed on the server side,
-        # this pattern only recognizes the IP address form
-        r'\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}'
-        t.value = str(t.value)
+    # Note that the expression for options has to be before the
+    # expression for words because the generated parser tries them in
+    # order, and the expression for words is a supperset of the
+    # expression for options. If the expression for words goes first
+    # then options are never matched.
+
+    def t_OPTION(self, t):
+        r'-(-[a-zA-Z_][a-zA-Z0-9_]*)+'
         return t
 
-    def t_UUID(self, t):
-        
r'[a-fA-F0-9]{8}-[a-fA-F0-9]{4}-[a-fA-F0-9]{4}-[a-fA-F0-9]{4}-[a-fA-F0-9]{12}'
-        t.value = str(t.value)
+    def t_WORD(self, t):
+        r'[^ \n\t"\'<>|!\\#;]+'
+        try:
+            t.value = int(t.value)
+        except ValueError:
+            pass
         return t
 
     def t_STRING(self, t):
@@ -68,11 +72,6 @@
         else:
             t.value = t.value[1:-1]
         t.value = t.value.replace('\\\n', '')
-        return t
-
-    def t_NUMBER(self, t):
-        r'[-+]?\d+'
-        t.value = int(t.value)
         return t
 
     def t_LTLT(self, t):
@@ -177,11 +176,8 @@
         p[0] = p[1]
 
     def p_argument(self, p):
-        """argument : IPADDR
-                    | UUID
-                    | WORD
+        """argument : WORD
                     | STRING
-                    | NUMBER
         """
         p[0] = p[1]
 
@@ -195,11 +191,8 @@
             p[0] = (p[1], p[3])
 
     def p_option_value(self, p):
-        """option_value : IPADDR
-                        | UUID
-                        | WORD
+        """option_value : WORD
                         | STRING
-                        | NUMBER
                         | empty
         """
         p[0] = p[1]


-- 
To view, visit http://gerrit.ovirt.org/29722
To unsubscribe, visit http://gerrit.ovirt.org/settings

Gerrit-MessageType: newchange
Gerrit-Change-Id: If670144751fdb9564380a12fb73596ecbaa47ee9
Gerrit-PatchSet: 1
Gerrit-Project: ovirt-engine-cli
Gerrit-Branch: master
Gerrit-Owner: Juan Hernandez <juan.hernan...@redhat.com>
_______________________________________________
Engine-patches mailing list
Engine-patches@ovirt.org
http://lists.ovirt.org/mailman/listinfo/engine-patches

Reply via email to