Commit graph

15 commits

Author SHA1 Message Date
Timothy Flynn fd8ccedf2b AK: Add GenericLexer API to consume an escaped Unicode code point
This parsing is already duplicated between LibJS and LibRegex, and will
shortly be needed in more places in those libraries. Move it to AK to
prevent further duplication.

This API will consume escaped Unicode code points of the form:
    \\u{code point}
    \\unnnn (where each n is a hexadecimal digit)
    \\unnnn\\unnnn (where the two escaped values are a surrogate pair)
2021-08-19 23:49:25 +02:00
Idan Horowitz 39a9cf4bb4 AK: Add a retreat(count) method to GenericLexer
This method can be used to rewind a constant amount backwards in the
source instead of one by one with retract()
2021-07-12 19:05:17 +01:00
Lenny Maiorani 254e010c75 AK/GenericLexer: constexpr where possible
Problem:
- Much of the `GenericLexer` can be `constexpr`, but is not.

Solution:
- Make it `constexpr` and de-duplicate code.
- Extend some of `StringView` with `constexpr` to support.
- Add tests to ensure `constexpr` behavior.

Note:
- Construction of `StringView` from pointer and length is not
  `constexpr`-compatible at the moment because the VERIFY cannot be,
  yet.
2021-04-22 20:27:21 +02:00
Brian Gianforcaro 1682f0b760 Everything: Move to SPDX license identifiers in all files.
SPDX License Identifiers are a more compact / standardized
way of representing file license information.

See: https://spdx.dev/resources/use/#identifiers

This was done with the `ambr` search and replace tool.

 ambr --no-parent-ignore --key-from-file --rep-from-file key.txt rep.txt *
2021-04-22 11:22:27 +02:00
Linus Groh 1daa5158eb AK: Add GenericLexer::retreat()
This allows going back one character at a time, and then re-consume
previously consumed chars.
The code I need this for looks something like this:

    ASSERT(lexer.consume_specific('\\'));
    if (lexer.next_is("foo"))
        ...
    lexer.retreat();
    lexer.consume_escaped_character();  // This expects lexer.peek() == '\\'
2020-10-29 11:52:31 +01:00
AnotherTest 27040e65eb AK: Add `GenericLexer::consume_escaped_character()'
...and use it in `consume_and_unescape_string()'.
2020-10-22 23:49:51 +02:00
asynts 6351a56d27 AK+Format: Do some housekeeping in the format implementation. 2020-10-02 20:48:19 +02:00
Benoît Lormeau f0f6b09acb AK: Remove the ctype adapters and use the actual ctype functions instead
This finally takes care of the kind-of excessive boilerplate code that were the
ctype adapters. On the other hand, I had to link `LibC/ctype.cpp` to the Kernel
(for `AK/JsonParser.cpp` and `AK/Format.cpp`). The previous commit actually makes
sense now: the `string.h` includes in `ctype.{h,cpp}` would require to link more LibC
stuff to the Kernel when it only needs the `_ctype_` array of `ctype.cpp`, and there
wasn't any string stuff used in ctype.
Instead of all this I could have put static derivatives of `is_any_of()` in the
concerned AK files, however that would have meant more boilerplate and workarounds;
so I went for the Kernel approach.
2020-09-27 21:15:25 +02:00
Benoit Lormeau e4da2875c5 AK: Use templates instead of Function for Conditions in the GenericLexer
Since commit 1ec59f28ce turns the ctype macros
into functions we can now feed them directly to a GenericLexer! This will lead to
removing the ctype adapters that were kind-of excessive boilerplate, but needed as
the Kernel doesn't compile with the LibC.
2020-09-27 21:15:25 +02:00
Benoit Lormeau 8f34b493e4 AK: Enhance GenericLexer's string consumption
The `consume_quoted_string()` can now take an escape character. This allows it
(for example) to capture a string's enclosing quotes. The escape character is
optional by default.

You can also consume and unescape a quoted string with the eponymous method
`consume_and_unescape_string()`. It takes an escape character as parameter
(backslash by default). It builds a String in which common escape sequences
get... unescaped :^) (e.g. \n, \r, \t...).
2020-09-26 17:17:53 +02:00
Benoit Lormeau 66481ad279 AK: Added explanatory comments in GenericLexer.h 2020-09-26 17:17:53 +02:00
asynts 84d276dba0 AK: Add GenericLexer::remaining.
This is useful for debugging with printf :^).
2020-09-26 00:00:50 +02:00
AnotherTest 441807f96d AK: Add is_any_of(StringView) to GenericLexer 2020-08-31 23:05:58 +02:00
Nico Weber 064159d215 LibWeb: Use GenericLexer in WrapperGenerator 2020-08-21 16:01:48 +02:00
Benoît Lormeau 7b356c33cb
AK: Add a GenericLexer and extend the JsonParser with it (#2696) 2020-08-09 11:34:26 +02:00