bug-bison
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

RFC: "count_chars" function for locations


From: Frank Heckenbach
Subject: RFC: "count_chars" function for locations
Date: Tue, 05 Mar 2019 02:17:04 +0100

Hi,

for a simple parser I'm writing, I have a scanner rule that can
match multi-line input, in particular, C-style "/* ... */" comments.
(Yes, I know that there are alternative ways to scan such comments,
involving flex states etc., but I want to keep things simple, and
even the purported flex performance issues with large matches don't
seem too matter much on modern machines until one gets into
megabytes long comments. ;)

Obviously, the simple location tracking using columns() and lines()
won't work if the match can contain newlines. So I use the following
simple function instead. (Yes again, I know, it's another pass over
the text, but as usual, lexing is the fastest part, so it doesn't
really matter to me, but I've added a note about it.)

Since this may be of interest to others, you might want to add this
function to location.cc. (It doesn't cost anything if it's not
used.) If you prefer another name instead of "count_chars", just
change it; I'm not attached to this name, just didn't have a better
idea ATM.

  class position:

    /// Advance the position according to the
    /// content of the N characters starting from S.
    /// Note: This implies an additional O(N) pass over the text.
    void count_chars (const char *s, size_t n)
    {
      while (n--)
        if (*s++ == '\n')
          lines ();
        else
          columns ();
    }

  class location:

    /// Extend the current location according to the
    /// content of the N characters starting from S.
    /// Note: This implies an additional O(N) pass over the text.
    void count_chars (const char *s, size_t n)
    {
      end.count_chars (s, n);
    }

Regards,
Frank



reply via email to

[Prev in Thread] Current Thread [Next in Thread]