Commit graph

88 commits

Author SHA1 Message Date
Carl Worth
681afbc855 Perform macro by replacing tokens in original list.
We take the results of macro expansion and splice them into the
original token list over which we are iterating. This makes it easy
for function-like macro invocations to find their arguments since they
are simply subsequent tokens on the list.

This fixes the recently-introduced regressions (tests 55 and 56) and
also passes new tests 60 and 61 introduced to strees this feature,
(with macro-argument parentheses split between a macro value and the
textual input).
2010-05-28 15:10:27 -07:00
Carl Worth
3c93d39705 Simplify calling conventions of functions under expand_token_list_onto.
We previously had a confusing thing where _expand_token_onto would
return a non-zero value to indicate that the caller should then call
_expand_function_onto. It's much cleaner for _expand_token_onto to
just do what's needed and call the necessary function.
2010-05-28 08:17:46 -07:00
Carl Worth
95ec433d59 Revert "Add support for an object-to-function chain with the parens in the content."
This reverts commit 7db2402a80

It doesn't revert the new test case from that commit, just the
extremely ugly second-pass implementation.
2010-05-28 08:02:07 -07:00
Carl Worth
050e3ded1e Implement token pasting of integers.
To do this correctly, we change the lexer to lex integers as string values,
(new token type of INTEGER_STRING), and only convert to integer values when
evaluating an expression value.

Add a new test case for this, (which does pass now).
2010-05-27 14:38:20 -07:00
Carl Worth
85b50e840d Add placeholder tokens to support pasting with empty arguments.
Along with a passing test to verify that this works.
2010-05-27 14:01:18 -07:00
Carl Worth
a19297b26e Provide support for empty arguments in macro invocations.
For this we always add a new argument to the argument list as soon as
possible, without waiting until we see some argument token. This does
mean we need to take some extra care when comparing the number of
arguments with the number of expected arguments. In addition to
matching numbers, we also support one (empty) argument when zero
arguments are expected.

Add a test case here for this, which does pass.
2010-05-27 13:29:19 -07:00
Carl Worth
a65cf7b1d2 Make two list-processing functions do nothing with an empty list.
This just makes these functions easier to understand all around.  In
the case of _token_list_append_list this is an actual bug fix, (where
append an empty list onto a non-empty list would previously scramble
the tail pointer of the original list).
2010-05-27 11:55:36 -07:00
Carl Worth
dd7490093d Avoid treating an expanded comma as an argument separator.
That is, a function-like invocation foo(x) is valid as a
single-argument invocation even if 'x' is a macro that expands into a
value with a comma. Add a new COMMA_FINAL token type to handle this,
and add a test for this case, (which passes).
2010-05-27 10:12:33 -07:00
Carl Worth
7db2402a80 Add support (and test) for an object-to-function chain with the parens in the content.
That is, the following case:

	#define foo(x) (x)
	#define bar
	bar(baz)

which now works with this (ugly) commit.

I definitely want to come up with something cleaner than this.
2010-05-26 17:01:57 -07:00
Carl Worth
95951ea7bb Treat newlines as space when invoking a function-like macro invocation.
This adds three new pieces of state to the parser, (is_control_line,
newline_as_space, and paren_count), and a large amount of messy
code. I'd definitely like to see a cleaner solution for this.

With this fix, the "define-func-extra-newlines" now passes so we put
it back to test #26 where it was originally (lately it has been known
as test #55).

Also, we tweak test 25 slightly. Previously this test was ending a
file function-like macro name that was not actually a macro (not
followed by a left parenthesis). As is, this fix was making that test
fail because the text_line production expects to see a terminating
NEWLINE, but that NEWLINE is now getting turned into a SPACE here.

This seems unlikely to be a problem in the wild, (function macros
being used in a non-macro sense seems rare enough---but more than
likely they won't happen at the end of a file). Still, we document
this shortcoming in the README.
2010-05-26 16:04:31 -07:00
Carl Worth
0324cad796 All macro lookups should be of type macro_t, not string_list_t.
This is what I get for using a non-type-safe hash-table implementation.
2010-05-26 15:53:05 -07:00
Carl Worth
8e82fcb070 Implement (and test) support for macro expansion within conditional expressions.
To do this we have split the existing "HASH_IF expression" into two
productions:

First is HASH_IF pp_tokens which simply constructs a list of tokens.

Then, with that resulting token list, we first evaluate all DEFINED
operator tokens, then expand all macros, and finally start lexing from
the resulting token list. This brings us to the second production,
IF_EXPANDED expression

This final production works just like our previous "HASH_IF
expression", evaluating a constant integer expression.

The new test (54) added for this case now passes.
2010-05-26 11:15:21 -07:00
Carl Worth
8fed1cddae stash 2010-05-26 09:32:12 -07:00
Carl Worth
ad0dee6bb0 Implement token pasting.
Which makes test 40 now pass.
2010-05-26 09:04:50 -07:00
Carl Worth
ce540f2571 Rename identifier from 'i' to 'node'.
Now that we no longer have nested for loops with 'i' and 'j' we can
use the 'node' that we already have.
2010-05-26 08:30:36 -07:00
Carl Worth
63909fc196 Remove some stale token types.
All the code referencing these was removed some time ago.
2010-05-26 08:16:56 -07:00
Carl Worth
ec4ada01c0 Prevent unexpanded macros from being expanded again in the future.
With this fix, tests 37 - 39 now pass.
2010-05-26 08:15:49 -07:00
Carl Worth
b1ae61a2ee Fix a typo in a comment.
Always better to use proper grammar in our grammar.
2010-05-26 08:10:38 -07:00
Carl Worth
d5cd40343f Expand macro arguments before performing argument substitution.
As required by the C99 specification of the preprocessor.

With this fix, tests 33 through 36 now pass.
2010-05-26 08:09:29 -07:00
Carl Worth
0197e9b64f Change macro expansion to append onto token lists rather than printing directly.
This doesn't change any functionality here, but will allow us to make
future changes that were not possible with direct printing.
Specifically, we need to expand macros within macro arguments before
performing argument substitution. And *that* expansion cannot result
in immediate printing.
2010-05-26 08:05:55 -07:00
Carl Worth
c0607d573e Check active expansions before expanding a function-like macro invocation.
With this fix, test 32 no longer recurses infinitely, but now passes.
2010-05-26 08:01:42 -07:00
Carl Worth
10ae438399 Avoid getting extra trailing whitespace from macros.
This trailing whitespace was coming from macro definitions and from
macro arguments. We fix this with a little extra state in the
token_list. It now remembers the last non-space token added, so that
these can be trimmed off just before printing the list.

With this fix test 23 now passes. Tests 24 and 25 are also passing,
but they probbably would ahve before this fix---just that they weren't
being run earlier.
2010-05-25 20:39:33 -07:00
Carl Worth
5aa7ea0809 Remove a bunch of old code and give the static treatment to what's left.
We're no longer using the expansion stack, so its functions can go
along with most of the body of glcpp_parser_lex that was using it.
2010-05-25 18:39:43 -07:00
Carl Worth
652fa272ea Avoid swallowing initial left parenthesis from nested macro invocation.
We weren't including this left parenthesis in the argument's token
list so the nested function invocation wasn not being recognized.

With this fix, tests 21 and 22 now pass.
2010-05-25 17:45:22 -07:00
Carl Worth
c7581c2e6e Ignore separating whitespace at the beginning of a macro argument.
This causes test 16 to pass. Tests 17-20 are also passing now, (though
they would probably have passed before this change and simply weren't
being run yet).
2010-05-25 17:41:07 -07:00
Carl Worth
9ce18cf983 Implement substitution of function parameters in macro calls.
This makes tests 16 - 19 pass.
2010-05-25 17:32:21 -07:00
Carl Worth
e9397867dd Collapse multiple spaces in input down to a single space.
This is what gcc does, and it's actually less work to do
this. Previously we were having to save the contents of space tokens
as a string, but we don't need to do that now.

We extend test #0 to exercise this feature here.
2010-05-25 17:08:07 -07:00
Carl Worth
f34a0009dd Pass through literal space values from replacement lists.
This makes test 15 pass and also dramatically simplifies the lexer.

We were previously using a CONTROL state in the lexer to only emit
SPACE tokens when on text lines. But that's not actually what we
want. We need SPACE tokens in the replacement lists as well. Instead
of a lexer state for this, we now simply set a "space_tokens" flag
whenever we start constructing a pp_tokens list and clear the flag
whenever we see a '#' introducing a directive.

Much cleaner this way.
2010-05-25 17:06:08 -07:00
Carl Worth
b1854fdfb6 Implement simplified substitution for function-like macro invocation.
This supports function-like macro invocation but without any argument
substitution. This now makes test 11 through 14 pass.
2010-05-25 16:28:26 -07:00
Carl Worth
e6fb7827c9 Implement #undef.
Which is as simple as copying the former action back from the git
history.

Now all tests through test 11 pass.
2010-05-25 15:28:58 -07:00
Carl Worth
ae6517f4a8 Implement expansion of object-like macros.
For this we add an "active" string_list_t to the parser. This makes
the current expansion_list_t in the parser obsolete, but we don't
remove that yet.

With this change we can now start passing some actual tests, so we
turn on real testing in the test suite again. I expect to implement
things more or less in the same order as before, so the test suite now
halts on first error.

With this change the first 8 tests in the suite pass, (object-like
macros with chaining and recursion).
2010-05-25 15:24:59 -07:00
Carl Worth
9fb8b7a495 Make the lexer pass whitespace through (as OTHER tokens) for text lines.
With this change, we can recreate the original text-line input
exactly. Previously we were inserting a space between every pair of
tokens so our output had a lot more whitespace than our input.

With this change, we can drop the "-b" option to diff and match the
input exactly.
2010-05-25 15:04:32 -07:00
Carl Worth
808401fd79 Store parsed tokens as token list and print all text lines.
Still not doing any macro expansion just yet. But it should be fairly
easy from here.
2010-05-25 14:52:43 -07:00
Carl Worth
80dc60b9c3 Delete some trailing whitespace.
This pernicious stuff managed to sneak in on us.
2010-05-25 14:42:00 -07:00
Carl Worth
3ff8167084 Starting over with the C99 grammar for the preprocessor.
This is a fresh start with a much simpler approach for the flex/bison
portions of the preprocessor. This isn't functional yet, (produces no
output), but can at least read all of our test cases without any parse
errors.

The grammar here is based on the grammar provided for the preprocessor
in the C99 specification.
2010-05-25 14:38:15 -07:00
Carl Worth
35419095f8 Switch to intmax_t (rather than int) for #if expressions
This is what the C99 specification demands. And the GLSL specification
says that we should follow the "standard C++" rules for #if condition
expressions rather than the GLSL rules, (which only support a 32-bit
integer).
2010-05-24 11:27:23 -07:00
Carl Worth
bcbd587b0f Implement all operators specified for GLSL #if expressions (with tests).
The operator coverage here is quite complete. The one big thing
missing is that we are not yet doing macro expansion in #if
lines. This makes the whole support fairly useless, so we plan to fix
that shortcoming right away.
2010-05-24 10:37:38 -07:00
Carl Worth
b20d33c5c6 Implement #if, #else, #elif, and #endif with tests.
So far the only expression implemented is a single integer literal,
but obviously that's easy to extend. Various things including nesting
are tested here.
2010-05-20 22:27:07 -07:00
Carl Worth
d8327e575d Implement (and add test) for token pasting.
This is *very* easy to implement now that macro arguments are pre-expanded.
2010-05-20 15:18:54 -07:00
Carl Worth
c10a51ba13 Pre-expand macro arguments at time of invocation.
Previously, we were using the same lexing stack as we use for macro
expansion to also expand macro arguments. Instead, we now do this
earlier by simply recursing over the macro-invocations replacement
list and constructing a new expanded list, (and pushing only *that*
onto the stack).

This is simpler, and also allows us to more easily implement token
pasting in the future.
2010-05-20 15:15:26 -07:00
Carl Worth
876e510bda Finish cleaning up whitespace differences.
The last remaining thing here was that when a line ended with a macro,
and the parser looked ahead to the newline token, the lexer was
printing that newline before the parser printed the expansion of the
macro.

The fix is simple, just make the lexer tell the parser that a newline
is needed, and the parser can wait until reducing a production to
print that newline.

With this, we now pass the entire test suite with simply "diff -u", so
we no longer have any diff options hiding whitespace bugs from
us. Hurrah!
2010-05-20 14:38:06 -07:00
Carl Worth
5a6b9a27fd Avoid printing a space at the beginning of lines in the output.
This fixes more differences compared to "gcc -E" so removes several
cases of erroneously failing test cases. The implementation isn't very
elegant, but it is functional.
2010-05-20 14:29:43 -07:00
Carl Worth
005b32061f Fix bug of consuming excess whitespace.
We fix this by moving printing up to the top-level "input" action and
tracking whether a space is needed between one token and the next.

This fixes all actual bugs in test-suite output, but does leave some
tests failing due to differences in the amount of whitespace produced,
(which aren't actual bugs per se).
2010-05-20 14:19:57 -07:00
Carl Worth
ff13cfed81 Remove unused function _print_string_list
The only good dead code is non-existing dead code.
2010-05-20 14:08:19 -07:00
Carl Worth
805ea6afe6 Add test (and fix) for a function argument of a macro that expands with a comma.
The fix here is quite simple (and actually only deletes code). When
expanding a macro, we don't return a ',' as a unique token type, but
simply let it fall through to the generic case.
2010-05-20 12:06:33 -07:00
Carl Worth
9f3d2c4e3d Add support for commas within parenthesized groups in function arguments.
The specification says that commas within a parenthesized group,
(that's not a function-like macro invocation), are passed through
literally and not considered argument separators in any outer macro
invocation.

Add support and a test for this case. This support makes a third
occurrence of the same "FUNC_MACRO (" shift/reduce conflict appear, so
expect that.

This change does introduce a fairly large copy/paste block in the
grammar which is unfortunate. Perhaps if I were more clever I'd find a
way to share the common pieces between argument and argument_or_comma.
2010-05-20 08:46:54 -07:00
Carl Worth
b569383bbd Avoid re-expanding a macro name that has once been rejected from expansion.
The specification of the preprocessor in C99 says that when we see a
macro name that we are already expanding that we refuse to expand it
now, (which we've done for a while), but also that we refuse to ever
expand it later if seen in other contexts at which it would be
legitimate to expand.

We add a test case for that here, and fix it to work. The fix takes
advantage of a new token_t value for tokens and argument words along
with the recently added IDENTIFIER_FINALIZED token type which
instructs the parser to not even look for another expansion.
2010-05-20 08:01:44 -07:00
Carl Worth
472524413d Use new token_list_t rather than string_list_t for macro values.
There's not yet any change in functionality here, (at least according
to the test suite). But we now have the option of specifying a type
for each string in the token list. This will allow us to finalize an
unexpanded macro name so that it won't be subjected to excess
expansion later.
2010-05-19 13:54:37 -07:00
Carl Worth
aaa9acbf10 Perform "re lexing" on string list values rathern than on text.
Previously, we would pass original strings back to the original lexer
whenever we needed to re-lex something, (such as an expanded macro or
a macro argument). Now, we instead parse the macro or argument
originally to a string list, and then re-lex by simply returning each
string from this list in turn.

We do this in the recently added glcpp_parser_lex function that sits
on top of the lower-level glcpp_lex that only deals with text.

This doesn't change any behavior (at least according to the existing
test suite which all still passes) but it brings us much closer to
being able to "finalize" an unexpanded macro as required by the
specification.
2010-05-19 13:28:24 -07:00
Carl Worth
71c59ec66b Remove unused NEWLINE token.
We fixed the lexer a while back to never return a NEWLINE token, but
negelcted to clean up this declaration.
2010-05-19 10:07:31 -07:00