Skip to content

Commit a219522

Browse files
committed
1 parent 3a920de commit a219522

22 files changed

+3063
-607
lines changed

src/main/c/yarp/include/prism.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
#include "prism/parser.h"
2222
#include "prism/prettyprint.h"
2323
#include "prism/regexp.h"
24+
#include "prism/static_literals.h"
2425
#include "prism/version.h"
2526

2627
#include <assert.h>

src/main/c/yarp/include/prism/ast.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
/* if you are looking to modify the */
66
/* template */
77
/******************************************************************************/
8+
89
/**
910
* @file ast.h
1011
*

src/main/c/yarp/include/prism/diagnostic.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -306,7 +306,10 @@ typedef enum {
306306
PM_WARN_AMBIGUOUS_SLASH,
307307
PM_WARN_EQUAL_IN_CONDITIONAL,
308308
PM_WARN_END_IN_METHOD,
309+
PM_WARN_DUPLICATED_HASH_KEY,
310+
PM_WARN_DUPLICATED_WHEN_CLAUSE,
309311
PM_WARN_FLOAT_OUT_OF_RANGE,
312+
PM_WARN_INTEGER_IN_FLIP_FLOP,
310313

311314
// This is the number of diagnostic codes.
312315
PM_DIAGNOSTIC_ID_LEN,

src/main/c/yarp/include/prism/node.h

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,17 @@
1010
#include "prism/parser.h"
1111
#include "prism/util/pm_buffer.h"
1212

13+
/**
14+
* Attempts to grow the node list to the next size. If there is already
15+
* capacity in the list, this function does nothing. Otherwise it reallocates
16+
* the list to be twice as large as it was before. If the reallocation fails,
17+
* this function returns false, otherwise it returns true.
18+
*
19+
* @param list The list to grow.
20+
* @return True if the list was successfully grown, false otherwise.
21+
*/
22+
bool pm_node_list_grow(pm_node_list_t *list);
23+
1324
/**
1425
* Append a new node onto the end of the node list.
1526
*
@@ -18,6 +29,22 @@
1829
*/
1930
void pm_node_list_append(pm_node_list_t *list, pm_node_t *node);
2031

32+
/**
33+
* Prepend a new node onto the beginning of the node list.
34+
*
35+
* @param list The list to prepend to.
36+
* @param node The node to prepend.
37+
*/
38+
void
39+
pm_node_list_prepend(pm_node_list_t *list, pm_node_t *node);
40+
41+
/**
42+
* Free the internal memory associated with the given node list.
43+
*
44+
* @param list The list to free.
45+
*/
46+
void pm_node_list_free(pm_node_list_t *list);
47+
2148
/**
2249
* Deallocate a node and all of its children.
2350
*

src/main/c/yarp/include/prism/options.h

Lines changed: 52 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,10 +76,50 @@ typedef struct {
7676
*/
7777
pm_options_version_t version;
7878

79+
/** A bitset of the various options that were set on the command line. */
80+
uint8_t command_line;
81+
7982
/** Whether or not the frozen string literal option has been set. */
8083
bool frozen_string_literal;
8184
} pm_options_t;
8285

86+
/**
87+
* A bit representing whether or not the command line -a option was set. -a
88+
* splits the input line $_ into $F.
89+
*/
90+
static const uint8_t PM_OPTIONS_COMMAND_LINE_A = 0x1;
91+
92+
/**
93+
* A bit representing whether or not the command line -e option was set. -e
94+
* allow the user to specify a script to be executed. This is necessary for
95+
* prism to know because certain warnings are not generated when -e is used.
96+
*/
97+
static const uint8_t PM_OPTIONS_COMMAND_LINE_E = 0x2;
98+
99+
/**
100+
* A bit representing whether or not the command line -l option was set. -l
101+
* chomps the input line by default.
102+
*/
103+
static const uint8_t PM_OPTIONS_COMMAND_LINE_L = 0x4;
104+
105+
/**
106+
* A bit representing whether or not the command line -n option was set. -n
107+
* wraps the script in a while gets loop.
108+
*/
109+
static const uint8_t PM_OPTIONS_COMMAND_LINE_N = 0x8;
110+
111+
/**
112+
* A bit representing whether or not the command line -p option was set. -p
113+
* prints the value of $_ at the end of each loop.
114+
*/
115+
static const uint8_t PM_OPTIONS_COMMAND_LINE_P = 0x10;
116+
117+
/**
118+
* A bit representing whether or not the command line -x option was set. -x
119+
* searches the input file for a shebang that matches the current Ruby engine.
120+
*/
121+
static const uint8_t PM_OPTIONS_COMMAND_LINE_X = 0x20;
122+
83123
/**
84124
* Set the filepath option on the given options struct.
85125
*
@@ -112,6 +152,14 @@ PRISM_EXPORTED_FUNCTION void pm_options_encoding_set(pm_options_t *options, cons
112152
*/
113153
PRISM_EXPORTED_FUNCTION void pm_options_frozen_string_literal_set(pm_options_t *options, bool frozen_string_literal);
114154

155+
/**
156+
* Sets the command line option on the given options struct.
157+
*
158+
* @param options The options struct to set the command line option on.
159+
* @param command_line The command_line value to set.
160+
*/
161+
PRISM_EXPORTED_FUNCTION void pm_options_command_line_set(pm_options_t *options, uint8_t command_line);
162+
115163
/**
116164
* Set the version option on the given options struct by parsing the given
117165
* string. If the string contains an invalid option, this returns false.
@@ -186,7 +234,10 @@ PRISM_EXPORTED_FUNCTION void pm_options_free(pm_options_t *options);
186234
* | `4` | the length the encoding |
187235
* | ... | the encoding bytes |
188236
* | `1` | frozen string literal |
189-
* | `1` | suppress warnings |
237+
* | `1` | -p command line option |
238+
* | `1` | -n command line option |
239+
* | `1` | -l command line option |
240+
* | `1` | -a command line option |
190241
* | `1` | the version |
191242
* | `4` | the number of scopes |
192243
* | ... | the scopes |

src/main/c/yarp/include/prism/parser.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -703,6 +703,9 @@ struct pm_parser {
703703
/** The version of prism that we should use to parse. */
704704
pm_options_version_t version;
705705

706+
/** The command line flags given from the options. */
707+
uint8_t command_line;
708+
706709
/** Whether or not we're at the beginning of a command. */
707710
bool command_start;
708711

Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
/**
2+
* @file static_literals.h
3+
*
4+
* A set of static literal nodes that can be checked for duplicates.
5+
*/
6+
#ifndef PRISM_STATIC_LITERALS_H
7+
#define PRISM_STATIC_LITERALS_H
8+
9+
#include "prism/defines.h"
10+
#include "prism/ast.h"
11+
#include "prism/node.h"
12+
#include "prism/parser.h"
13+
14+
#include <assert.h>
15+
#include <stdbool.h>
16+
17+
/**
18+
* An internal hash table for a set of nodes.
19+
*/
20+
typedef struct {
21+
/** The array of nodes in the hash table. */
22+
pm_node_t **nodes;
23+
24+
/** The size of the hash table. */
25+
uint32_t size;
26+
27+
/** The space that has been allocated in the hash table. */
28+
uint32_t capacity;
29+
} pm_node_hash_t;
30+
31+
/**
32+
* Certain sets of nodes (hash keys and when clauses) check for duplicate nodes
33+
* to alert the user of potential issues. To do this, we keep a set of the nodes
34+
* that have been seen so far, and compare whenever we find a new node.
35+
*
36+
* We bucket the nodes based on their type to minimize the number of comparisons
37+
* that need to be performed.
38+
*/
39+
typedef struct {
40+
/**
41+
* This is the set of IntegerNode and SourceLineNode instances.
42+
*/
43+
pm_node_hash_t integer_nodes;
44+
45+
/**
46+
* This is the set of FloatNode instances.
47+
*/
48+
pm_node_hash_t float_nodes;
49+
50+
/**
51+
* This is the set of RationalNode and ImaginaryNode instances.
52+
*/
53+
pm_node_hash_t number_nodes;
54+
55+
/**
56+
* This is the set of StringNode and SourceFileNode instances.
57+
*/
58+
pm_node_hash_t string_nodes;
59+
60+
/**
61+
* This is the set of RegularExpressionNode instances.
62+
*/
63+
pm_node_hash_t regexp_nodes;
64+
65+
/**
66+
* This is the set of SymbolNode instances.
67+
*/
68+
pm_node_hash_t symbol_nodes;
69+
70+
/**
71+
* A pointer to the last TrueNode instance that was inserted, or NULL.
72+
*/
73+
pm_node_t *true_node;
74+
75+
/**
76+
* A pointer to the last FalseNode instance that was inserted, or NULL.
77+
*/
78+
pm_node_t *false_node;
79+
80+
/**
81+
* A pointer to the last NilNode instance that was inserted, or NULL.
82+
*/
83+
pm_node_t *nil_node;
84+
85+
/**
86+
* A pointer to the last SourceEncodingNode instance that was inserted, or
87+
* NULL.
88+
*/
89+
pm_node_t *source_encoding_node;
90+
} pm_static_literals_t;
91+
92+
/**
93+
* Add a node to the set of static literals.
94+
*
95+
* @param parser The parser that created the node.
96+
* @param literals The set of static literals to add the node to.
97+
* @param node The node to add to the set.
98+
* @return A pointer to the node that is being overwritten, if there is one.
99+
*/
100+
pm_node_t * pm_static_literals_add(const pm_parser_t *parser, pm_static_literals_t *literals, pm_node_t *node);
101+
102+
/**
103+
* Free the internal memory associated with the given static literals set.
104+
*
105+
* @param literals The set of static literals to free.
106+
*/
107+
void pm_static_literals_free(pm_static_literals_t *literals);
108+
109+
#endif

src/main/c/yarp/include/prism/util/pm_integer.h

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
#define PRISM_NUMBER_H
88

99
#include "prism/defines.h"
10+
#include "prism/util/pm_buffer.h"
1011

1112
#include <assert.h>
1213
#include <stdbool.h>
@@ -93,6 +94,25 @@ PRISM_EXPORTED_FUNCTION void pm_integer_parse(pm_integer_t *integer, pm_integer_
9394
*/
9495
size_t pm_integer_memsize(const pm_integer_t *integer);
9596

97+
/**
98+
* Compare two integers. This function returns -1 if the left integer is less
99+
* than the right integer, 0 if they are equal, and 1 if the left integer is
100+
* greater than the right integer.
101+
*
102+
* @param left The left integer to compare.
103+
* @param right The right integer to compare.
104+
* @return The result of the comparison.
105+
*/
106+
int pm_integer_compare(const pm_integer_t *left, const pm_integer_t *right);
107+
108+
/**
109+
* Convert an integer to a decimal string.
110+
*
111+
* @param buffer The buffer to append the string to.
112+
* @param integer The integer to convert to a string.
113+
*/
114+
PRISM_EXPORTED_FUNCTION void pm_integer_string(pm_buffer_t *buffer, const pm_integer_t *integer);
115+
96116
/**
97117
* Free the internal memory of an integer. This memory will only be allocated if
98118
* the integer exceeds the size of a single node in the linked list.

src/main/c/yarp/src/diagnostic.c

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -306,9 +306,12 @@ static const pm_diagnostic_data_t diagnostic_messages[PM_DIAGNOSTIC_ID_LEN] = {
306306
[PM_WARN_AMBIGUOUS_FIRST_ARGUMENT_PLUS] = { "ambiguous first argument; put parentheses or a space even after `+` operator", PM_WARNING_LEVEL_VERBOSE },
307307
[PM_WARN_AMBIGUOUS_PREFIX_STAR] = { "ambiguous `*` has been interpreted as an argument prefix", PM_WARNING_LEVEL_VERBOSE },
308308
[PM_WARN_AMBIGUOUS_SLASH] = { "ambiguous `/`; wrap regexp in parentheses or add a space after `/` operator", PM_WARNING_LEVEL_VERBOSE },
309+
[PM_WARN_DUPLICATED_HASH_KEY] = { "key %.*s is duplicated and overwritten on line %" PRIi32, PM_WARNING_LEVEL_DEFAULT },
310+
[PM_WARN_DUPLICATED_WHEN_CLAUSE] = { "duplicated 'when' clause with line %" PRIi32 " is ignored", PM_WARNING_LEVEL_VERBOSE },
309311
[PM_WARN_EQUAL_IN_CONDITIONAL] = { "found `= literal' in conditional, should be ==", PM_WARNING_LEVEL_DEFAULT },
310312
[PM_WARN_END_IN_METHOD] = { "END in method; use at_exit", PM_WARNING_LEVEL_DEFAULT },
311-
[PM_WARN_FLOAT_OUT_OF_RANGE] = { "Float %.*s%s out of range", PM_WARNING_LEVEL_VERBOSE }
313+
[PM_WARN_FLOAT_OUT_OF_RANGE] = { "Float %.*s%s out of range", PM_WARNING_LEVEL_VERBOSE },
314+
[PM_WARN_INTEGER_IN_FLIP_FLOP] = { "integer literal in flip-flop", PM_WARNING_LEVEL_DEFAULT }
312315
};
313316

314317
static inline const char *

0 commit comments

Comments
 (0)