Loading ...
Sorry, an error occurred while loading the content.

Syntax: separating operators from number signs

Expand Messages
  • Alejandro Pulver
    Hello, I am new to this list, but not to Vim (however I still have much to learn). I have a problem making a syntax plugin for Redcode assembly files. I have a
    Message 1 of 9 , Apr 1, 2005
    • 0 Attachment
      Hello,

      I am new to this list, but not to Vim (however I still have much to
      learn). I have a problem making a syntax plugin for Redcode assembly
      files.

      I have a problem to separate operators (+, -) from the sign of a number
      (positive or negative). In this example both are marked in Vim as
      operators, but one is an operator and the other a sign:

      mov 0, dst+1 -- an addition operator
      mov 0, -1 -- a negative sign

      I use in my syntax file:

      syntax match redcodeNumber /[-+]\?[0-9]\d*/
      syntax match redcodeExprArithOp /+\|-\|\/\|\*\|%/

      How can I solve this?

      NOTE: the number constants can be preceded by the following modifiers: #
      $ @ * < > { }. Like: "add #-3, dst".

      Thanks and Best Regards,
      Ale
    • Mikolaj Machowski
      ... Put space in syntax definition here (maybe zero-width) ... ^ s @
      Message 2 of 9 , Apr 2, 2005
      • 0 Attachment
        Dnia piątek, 1 kwietnia 2005 22:44, Alejandro Pulver napisał:
        > Hello,
        >
        > I am new to this list, but not to Vim (however I still have much to
        > learn). I have a problem making a syntax plugin for Redcode assembly
        > files.
        >
        > I have a problem to separate operators (+, -) from the sign of a number
        > (positive or negative). In this example both are marked in Vim as
        > operators, but one is an operator and the other a sign:
        >
        > mov 0, dst+1 -- an addition operator
        > mov 0, -1 -- a negative sign

        Put space in syntax definition here (maybe zero-width)
        >
        > I use in my syntax file:
        >
        > syntax match redcodeNumber /[-+]\?[0-9]\d*/
        ^ \s\@<=

        And you can replace [0-9]\d* with \d\+
        Note: I am never sure which zero-width operator is good, so check :h /\@
        before writing 'this is not working' :)

        > syntax match redcodeExprArithOp /+\|-\|\/\|\*\|%/

        Here also you could add: \s\@<! before elements or explicitly
        [$@*<>{}]\@<! .

        m.

        --
        LaTeX + Vim = http://vim-latex.sourceforge.net/
        Vim-list(s) Users Map: (last change 12 Feb)
        http://skawina.eu.org/mikolaj/vimlist
        CLEWN - http://clewn.sf.net
      • Alejandro Pulver
        On Sat, 2 Apr 2005 12:27:10 +0200 ... Hello, Thank you for your reply. I ended up with the following to match numbers (is it fine? can it be optimized?): /[
        Message 3 of 9 , Apr 2, 2005
        • 0 Attachment
          On Sat, 2 Apr 2005 12:27:10 +0200
          Mikolaj Machowski <mikmach@...> wrote:

          > Dnia pi±tek, 1 kwietnia 2005 22:44, Alejandro Pulver napisa³:
          > > Hello,
          > >
          > > I am new to this list, but not to Vim (however I still have much to
          > > learn). I have a problem making a syntax plugin for Redcode assembly
          > > files.
          > >
          > > I have a problem to separate operators (+, -) from the sign of a
          > > number(positive or negative). In this example both are marked in Vim
          > > as operators, but one is an operator and the other a sign:
          > >
          > > mov 0, dst+1 -- an addition operator
          > > mov 0, -1 -- a negative sign
          >
          > Put space in syntax definition here (maybe zero-width)
          > >
          > > I use in my syntax file:
          > >
          > > syntax match redcodeNumber /[-+]\?[0-9]\d*/
          > ^ \s\@<=
          >
          > And you can replace [0-9]\d* with \d\+
          > Note: I am never sure which zero-width operator is good, so check :h
          > /\@ before writing 'this is not working' :)
          >
          > > syntax match redcodeExprArithOp /+\|-\|\/\|\*\|%/
          >
          > Here also you could add: \s\@<! before elements or explicitly
          > [$@*<>{}]\@<! .
          >
          > m.
          >

          Hello,

          Thank you for your reply.

          I ended up with the following to match numbers (is it fine? can it be
          optimized?):

          /[ \t#$@*<>{},]\@<=[-+]\?\d\+\|\d\+/

          And this to match the - and + operators (can it be optimized?):

          /[ \t\s#$@*<>{},]\@<![+-]\|[ \s\t][+-][ \s\t]/

          Thanks and Best Regards,
          Ale
        • Mikolaj Machowski
          ... s matches any white-space character including tabs, so [ s t] can be replaced by just s. m.
          Message 4 of 9 , Apr 2, 2005
          • 0 Attachment
            Dnia sobota, 2 kwietnia 2005 18:46, Alejandro Pulver napisał:
            > I ended up with the following to match numbers (is it fine? can it be
            > optimized?):
            >
            > /[ \t#$@*<>{},]\@<=[-+]\?\d\+\|\d\+/
            >
            > And this to match the - and + operators (can it be optimized?):
            >
            > /[ \t\s#$@*<>{},]\@<![+-]\|[ \s\t][+-][ \s\t]/

            \s matches any white-space character including tabs, so [ \s\t] can be
            replaced by just \s.

            m.
          • Alejandro Pulver
            On Sat, 2 Apr 2005 20:23:46 +0200 ... Hello, Thank you for your reply. I tried /[#$@* {}, s] @
            Message 5 of 9 , Apr 2, 2005
            • 0 Attachment
              On Sat, 2 Apr 2005 20:23:46 +0200
              Mikolaj Machowski <mikmach@...> wrote:

              > Dnia sobota, 2 kwietnia 2005 18:46, Alejandro Pulver napisał:
              > > I ended up with the following to match numbers (is it fine? can it
              > > be optimized?):
              > >
              > > /[ \t#$@*<>{},]\@<=[-+]\?\d\+\|\d\+/
              > >
              > > And this to match the - and + operators (can it be optimized?):
              > >
              > > /[ \t\s#$@*<>{},]\@<![+-]\|[ \s\t][+-][ \s\t]/
              >
              > \s matches any white-space character including tabs, so [ \s\t] can be
              > replaced by just \s.
              >
              > m.
              >

              Hello,

              Thank you for your reply.

              I tried /[#$@*<>{},\s]\@<=[-+]\?\d\+\|\d\+/ (\s instead of [ \t\s]) but
              if I have:

              dst: dat #0, # -3

              The '-' is not marked as a number. But if I have:

              dst: dat #0, #-3

              It is marked as a number.

              What is happening?

              Thanks and Best Regards,
              Ale

              P.S.: is there a way to define a group of atoms for using in a regular
              expression? Like grouping all the modifiers in one "variable" or "group"
              and put the group name in the regular expressions.
            • Mikolaj Machowski
              ... You cannot use s (or any other class) inside of [] ... Here catches operator rule. [] is always one char. Try / ([#$@* {},] s* ) @
              Message 6 of 9 , Apr 2, 2005
              • 0 Attachment
                Dnia sobota, 2 kwietnia 2005 20:48, Alejandro Pulver napisał:
                > Hello,
                >
                > Thank you for your reply.
                >
                > I tried /[#$@*<>{},\s]\@<=[-+]\?\d\+\|\d\+/ (\s instead of [ \t\s]) but

                You cannot use \s (or any other class) inside of []
                > if I have:
                >
                > dst: dat #0, # -3

                Here catches operator rule. [] is always one char. Try
                /\([#$@*<>{},]\s*\)\@<=[-+]\?\d\+\|\d\+/

                > P.S.: is there a way to define a group of atoms for using in a regular
                > expression? Like grouping all the modifiers in one "variable" or "group"
                > and put the group name in the regular expressions.

                You could try to put it into variable and resolve them in syntax file
                with exe (:h :exe) but this is tricky and can significantly slow down
                Vim.

                m.


                --
                [`] 2005-04-02 21:37
              • Alejandro Pulver
                ... Hello, Thank you. Best Regards, Ale
                Message 7 of 9 , Apr 3, 2005
                • 0 Attachment
                  > Dnia sobota, 2 kwietnia 2005 20:48, Alejandro Pulver napisaÅ?:
                  >> Hello,
                  >>
                  >> Thank you for your reply.
                  >>
                  >> I tried /[#$@*<>{},\s]\@<=[-+]\?\d\+\|\d\+/ (\s instead of [ \t\s]) but
                  >
                  > You cannot use \s (or any other class) inside of []
                  >> if I have:
                  >>
                  >> dst: dat #0, # -3
                  >
                  > Here catches operator rule. [] is always one char. Try
                  > /\([#$@*<>{},]\s*\)\@<=[-+]\?\d\+\|\d\+/
                  >
                  >> P.S.: is there a way to define a group of atoms for using in a regular
                  >> expression? Like grouping all the modifiers in one "variable" or "group"
                  >> and put the group name in the regular expressions.
                  >
                  > You could try to put it into variable and resolve them in syntax file
                  > with exe (:h :exe) but this is tricky and can significantly slow down
                  > Vim.
                  >
                  > m.
                  >
                  >
                  > --
                  > [`] 2005-04-02 21:37
                  >
                  >

                  Hello,

                  Thank you.

                  Best Regards,
                  Ale
                • drchip@campbellfamily.biz
                  ... * priority control: order (latest == highest), keywords have top priority, and nextgroup. So, with that in mind, please check out the following; I m sure
                  Message 8 of 9 , Apr 4, 2005
                  • 0 Attachment
                    Quoting Alejandro Pulver <alejandro@...>:

                    >...
                    > I have a problem to separate operators (+, -) from the sign of a number
                    > (positive or negative). In this example both are marked in Vim as
                    > operators, but one is an operator and the other a sign:
                    >
                    > mov 0, dst+1 -- an addition operator
                    > mov 0, -1 -- a negative sign
                    >...

                    * priority control: order (latest == highest), keywords have
                    top priority, and nextgroup.

                    So, with that in mind, please check out the following; I'm sure
                    that its notion of variables will need to be improved, but
                    at least its a start:

                    syn keyword redcodeKey mov
                    syn match redcodeVar "\h\w*" skipwhite nextgroup=redcodeOp
                    syn match redcodeNmbr "\d\+"
                    syn match redcodeOp "[-+]" contained
                    syn match redcodeSign "[-+]\ze\d\+"

                    hi link redcodeKey Statement
                    hi link redcodeVar Identifier
                    hi link redcodeOp Operator
                    hi link redcodeSign redcodeNmbr
                    hi link redcodeNmbr Number

                    Regards,
                    Chip Campbell
                  • Alejandro Pulver
                    On Mon, 4 Apr 2005 06:23:25 -0700 ... Hello, Thank you for your reply. I noticed that syntax keyword does not support nextgroup . The Redcode language has
                    Message 9 of 9 , Apr 4, 2005
                    • 0 Attachment
                      On Mon, 4 Apr 2005 06:23:25 -0700
                      drchip@... wrote:

                      > Quoting Alejandro Pulver <alejandro@...>:
                      >
                      > >...
                      > > I have a problem to separate operators (+, -) from the sign of a
                      > > number(positive or negative). In this example both are marked in Vim
                      > > as operators, but one is an operator and the other a sign:
                      > >
                      > > mov 0, dst+1 -- an addition operator
                      > > mov 0, -1 -- a negative sign
                      > >...
                      >
                      > * priority control: order (latest == highest), keywords have
                      > top priority, and nextgroup.
                      >
                      > So, with that in mind, please check out the following; I'm sure
                      > that its notion of variables will need to be improved, but
                      > at least its a start:
                      >
                      > syn keyword redcodeKey mov
                      > syn match redcodeVar "\h\w*" skipwhite nextgroup=redcodeOp
                      > syn match redcodeNmbr "\d\+"
                      > syn match redcodeOp "[-+]" contained
                      > syn match redcodeSign "[-+]\ze\d\+"
                      >
                      > hi link redcodeKey Statement
                      > hi link redcodeVar Identifier
                      > hi link redcodeOp Operator
                      > hi link redcodeSign redcodeNmbr
                      > hi link redcodeNmbr Number
                      >
                      > Regards,
                      > Chip Campbell
                      >

                      Hello,

                      Thank you for your reply.

                      I noticed that "syntax keyword" does not support "nextgroup". The
                      Redcode language has instruction modifiers (they are the same for
                      all instructions): ".a", ".b", ".i", ".f", ".ab", ".ba". Like the
                      following:

                      step1: mov.i -1, 3
                      mov.f @3, 63
                      jmp -2

                      How can I implement them?

                      How can I be sure that there is one instruction with the two fields
                      (e.g. no more than two fields, no nonexistant instructions, possibly
                      labels in front)?

                      If I specify every valid combination with their respective highlighting,
                      can I make everything else be an error (in the Error group) or I have to
                      do individual groups (e.g. valid numbers and invalid numbers, valid
                      modifiers and invalid modifiers)?

                      Is it possible to use two different groups in a single pattern (without
                      having to write it twice)?

                      What syntax layout (groups, etc.) is more appropiate for this case?

                      [<label>][:] <instruction[.<modifier>]> <[<modifier>]field-a> [,
                      <[<modifier>]field-b>] [; <comment>]

                      [] -> optional

                      Thanks and Best Regards,
                      Ale
                    Your message has been successfully submitted and would be delivered to recipients shortly.