Home | History | Annotate | Line # | Download | only in indent
indent.h revision 1.178
      1  1.178  rillig /*	$NetBSD: indent.h,v 1.178 2023/06/08 06:47:13 rillig Exp $	*/
      2    1.1   kamil 
      3    1.1   kamil /*-
      4    1.1   kamil  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
      5    1.1   kamil  *
      6    1.1   kamil  * Copyright (c) 2001 Jens Schweikhardt
      7    1.1   kamil  * All rights reserved.
      8    1.1   kamil  *
      9    1.1   kamil  * Redistribution and use in source and binary forms, with or without
     10    1.1   kamil  * modification, are permitted provided that the following conditions
     11    1.1   kamil  * are met:
     12    1.1   kamil  * 1. Redistributions of source code must retain the above copyright
     13    1.1   kamil  *    notice, this list of conditions and the following disclaimer.
     14    1.1   kamil  * 2. Redistributions in binary form must reproduce the above copyright
     15    1.1   kamil  *    notice, this list of conditions and the following disclaimer in the
     16    1.1   kamil  *    documentation and/or other materials provided with the distribution.
     17    1.1   kamil  *
     18    1.1   kamil  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND
     19    1.1   kamil  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     20    1.1   kamil  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     21    1.1   kamil  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE
     22    1.1   kamil  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     23    1.1   kamil  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     24    1.1   kamil  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     25    1.1   kamil  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     26    1.1   kamil  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     27    1.1   kamil  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     28    1.1   kamil  * SUCH DAMAGE.
     29    1.1   kamil  */
     30   1.31  rillig /*-
     31   1.31  rillig  * SPDX-License-Identifier: BSD-4-Clause
     32   1.31  rillig  *
     33   1.31  rillig  * Copyright (c) 1985 Sun Microsystems, Inc.
     34   1.31  rillig  * Copyright (c) 1980, 1993
     35   1.31  rillig  *	The Regents of the University of California.  All rights reserved.
     36   1.31  rillig  * All rights reserved.
     37   1.31  rillig  *
     38   1.31  rillig  * Redistribution and use in source and binary forms, with or without
     39   1.31  rillig  * modification, are permitted provided that the following conditions
     40   1.31  rillig  * are met:
     41   1.31  rillig  * 1. Redistributions of source code must retain the above copyright
     42   1.31  rillig  *    notice, this list of conditions and the following disclaimer.
     43   1.31  rillig  * 2. Redistributions in binary form must reproduce the above copyright
     44   1.31  rillig  *    notice, this list of conditions and the following disclaimer in the
     45   1.31  rillig  *    documentation and/or other materials provided with the distribution.
     46   1.31  rillig  * 3. All advertising materials mentioning features or use of this software
     47   1.31  rillig  *    must display the following acknowledgement:
     48   1.31  rillig  *	This product includes software developed by the University of
     49   1.31  rillig  *	California, Berkeley and its contributors.
     50   1.31  rillig  * 4. Neither the name of the University nor the names of its contributors
     51   1.31  rillig  *    may be used to endorse or promote products derived from this software
     52   1.31  rillig  *    without specific prior written permission.
     53   1.31  rillig  *
     54   1.31  rillig  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     55   1.31  rillig  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     56   1.31  rillig  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     57   1.31  rillig  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     58   1.31  rillig  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     59   1.31  rillig  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     60   1.31  rillig  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     61   1.31  rillig  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     62   1.31  rillig  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     63   1.31  rillig  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     64   1.31  rillig  * SUCH DAMAGE.
     65   1.31  rillig  */
     66    1.1   kamil 
     67   1.98  rillig #include <ctype.h>
     68   1.20  rillig #include <stdbool.h>
     69   1.65  rillig #include <stdio.h>
     70   1.20  rillig 
     71   1.49  rillig typedef enum lexer_symbol {
     72  1.148  rillig 	lsym_eof,
     73  1.167  rillig 	lsym_preprocessing,	/* the initial '#' of a preprocessing line */
     74  1.148  rillig 	lsym_newline,
     75  1.148  rillig 	lsym_comment,		/* the initial '/ *' or '//' of a comment */
     76  1.163  rillig 	lsym_lparen,
     77  1.163  rillig 	lsym_lbracket,
     78  1.163  rillig 	lsym_rparen,
     79  1.163  rillig 	lsym_rbracket,
     80  1.148  rillig 	lsym_lbrace,
     81  1.148  rillig 	lsym_rbrace,
     82  1.148  rillig 	lsym_period,
     83  1.148  rillig 	lsym_unary_op,		/* e.g. '*', '&', '-' or leading '++' */
     84  1.178  rillig 	lsym_postfix_op,	/* trailing '++' or '--' */
     85  1.148  rillig 	lsym_binary_op,		/* e.g. '*', '&', '<<', '&&' or '/=' */
     86  1.148  rillig 	lsym_question,		/* the '?' from a '?:' expression */
     87  1.165  rillig 	lsym_colon_question,	/* the ':' from a '?:' expression */
     88  1.165  rillig 	lsym_colon_label,	/* the ':' after a label */
     89  1.165  rillig 	lsym_colon_other,	/* bit-fields, generic-association (C11),
     90  1.165  rillig 				 * enum-type-specifier (C23),
     91  1.165  rillig 				 * attribute-prefixed-token (C23),
     92  1.174  rillig 				 * pp-prefixed-parameter (C23 6.10) */
     93  1.148  rillig 	lsym_comma,
     94  1.148  rillig 	lsym_semicolon,
     95  1.148  rillig 	lsym_typedef,
     96  1.166  rillig 	lsym_modifier,		/* modifiers for types, functions, variables */
     97  1.148  rillig 	lsym_type_outside_parentheses,
     98  1.148  rillig 	lsym_type_in_parentheses,
     99  1.148  rillig 	lsym_tag,		/* 'struct', 'union' or 'enum' */
    100  1.167  rillig 	lsym_case,
    101  1.167  rillig 	lsym_default,
    102  1.148  rillig 	lsym_sizeof,
    103  1.148  rillig 	lsym_offsetof,
    104  1.148  rillig 	lsym_word,		/* identifier, constant or string */
    105  1.150  rillig 	lsym_funcname,		/* name of a function being defined */
    106  1.148  rillig 	lsym_do,
    107  1.148  rillig 	lsym_else,
    108  1.148  rillig 	lsym_for,
    109  1.148  rillig 	lsym_if,
    110  1.148  rillig 	lsym_switch,
    111  1.148  rillig 	lsym_while,
    112  1.167  rillig 	lsym_return,
    113   1.49  rillig } lexer_symbol;
    114   1.49  rillig 
    115  1.169  rillig /*
    116  1.169  rillig  * Structure of the source code, in terms of declarations, statements and
    117  1.169  rillig  * braces; used to determine the indentation level of these parts.
    118  1.169  rillig  */
    119   1.49  rillig typedef enum parser_symbol {
    120  1.169  rillig 	psym_0,			/* a placeholder; not stored on the stack */
    121  1.170  rillig 	psym_lbrace_block,	/* '{' for a block of code */
    122  1.170  rillig 	psym_lbrace_struct,	/* '{' in 'struct ... { ... }' */
    123  1.170  rillig 	psym_lbrace_union,	/* '{' in 'union ... { ... }' */
    124  1.170  rillig 	psym_lbrace_enum,	/* '{' in 'enum ... { ... }' */
    125  1.169  rillig 	psym_rbrace,		/* not stored on the stack */
    126  1.148  rillig 	psym_decl,
    127  1.148  rillig 	psym_stmt,
    128  1.148  rillig 	psym_stmt_list,
    129  1.148  rillig 	psym_for_exprs,		/* 'for' '(' ... ')' */
    130  1.148  rillig 	psym_if_expr,		/* 'if' '(' expr ')' */
    131  1.148  rillig 	psym_if_expr_stmt,	/* 'if' '(' expr ')' stmt */
    132  1.148  rillig 	psym_if_expr_stmt_else,	/* 'if' '(' expr ')' stmt 'else' */
    133  1.148  rillig 	psym_else,		/* 'else'; not stored on the stack */
    134  1.148  rillig 	psym_switch_expr,	/* 'switch' '(' expr ')' */
    135  1.148  rillig 	psym_do,		/* 'do' */
    136  1.148  rillig 	psym_do_stmt,		/* 'do' stmt */
    137  1.148  rillig 	psym_while_expr,	/* 'while' '(' expr ')' */
    138   1.49  rillig } parser_symbol;
    139   1.49  rillig 
    140  1.132  rillig /* A range of characters, not null-terminated. */
    141   1.31  rillig struct buffer {
    142  1.171  rillig 	char *s;
    143  1.171  rillig 	size_t len;
    144  1.148  rillig 	size_t cap;
    145   1.31  rillig };
    146   1.31  rillig 
    147   1.34  rillig extern FILE *input;
    148   1.34  rillig extern FILE *output;
    149   1.31  rillig 
    150  1.146  rillig /*
    151  1.146  rillig  * The current line from the input file, used by the lexer to generate tokens.
    152  1.171  rillig  * To read from the line, start at inp_p and continue up to and including the
    153  1.146  rillig  * next '\n'. To read beyond the '\n', call inp_skip or inp_next, which will
    154  1.146  rillig  * make the next line available, invalidating any pointers into the previous
    155  1.146  rillig  * line.
    156  1.146  rillig  */
    157  1.146  rillig extern struct buffer inp;
    158  1.171  rillig extern const char *inp_p;
    159  1.146  rillig 
    160   1.61  rillig extern struct buffer token;	/* the current token to be processed, is
    161  1.148  rillig 				 * typically copied to the buffer 'code', or in
    162  1.148  rillig 				 * some cases to 'lab'. */
    163   1.61  rillig 
    164   1.61  rillig extern struct buffer lab;	/* the label or preprocessor directive */
    165  1.140  rillig extern struct buffer code;	/* the main part of the current line of code,
    166  1.140  rillig 				 * containing declarations or statements */
    167   1.61  rillig extern struct buffer com;	/* the trailing comment of the line, or the
    168   1.83  rillig 				 * start or end of a multi-line comment, or
    169  1.148  rillig 				 * while in process_comment, a single line of a
    170  1.148  rillig 				 * multi-line comment */
    171   1.31  rillig 
    172   1.31  rillig extern struct options {
    173  1.148  rillig 	bool blanklines_around_conditional_compilation;
    174  1.148  rillig 	bool blank_line_after_decl_at_top;	/* this is vaguely similar to
    175  1.148  rillig 						 * blank_line_after_decl except
    176  1.148  rillig 						 * that it only applies to the
    177  1.148  rillig 						 * first set of declarations in
    178  1.148  rillig 						 * a procedure (just after the
    179  1.148  rillig 						 * first '{') and it causes a
    180  1.148  rillig 						 * blank line to be generated
    181  1.148  rillig 						 * even if there are no
    182  1.148  rillig 						 * declarations */
    183  1.148  rillig 	bool blank_line_after_decl;
    184  1.148  rillig 	bool blanklines_after_procs;
    185  1.148  rillig 	bool blanklines_before_block_comments;
    186  1.148  rillig 	bool break_after_comma;	/* whether to add a line break after each
    187  1.140  rillig 				 * declarator */
    188  1.148  rillig 	bool brace_same_line;	/* whether brace should be on same line as if,
    189   1.34  rillig 				 * while, etc */
    190  1.148  rillig 	bool blank_after_sizeof;	/* whether a blank should always be
    191  1.148  rillig 					 * inserted after sizeof */
    192  1.148  rillig 	bool comment_delimiter_on_blankline;
    193  1.148  rillig 	int decl_comment_column;	/* the column in which comments after
    194  1.148  rillig 					 * declarations should be put */
    195  1.148  rillig 	bool cuddle_else;	/* whether 'else' should cuddle up to '}' */
    196  1.148  rillig 	int continuation_indent;	/* the indentation between the edge of
    197  1.148  rillig 					 * code and continuation lines */
    198  1.148  rillig 	float case_indent;	/* The distance (measured in indentation
    199   1.31  rillig 				 * levels) to indent case labels from the
    200   1.31  rillig 				 * switch statement */
    201  1.148  rillig 	int comment_column;	/* the column in which comments to the right of
    202  1.148  rillig 				 * code should start */
    203  1.148  rillig 	int decl_indent;	/* indentation of identifier in declaration */
    204  1.172  rillig 	bool left_justify_decl;
    205  1.148  rillig 	int unindent_displace;	/* comments not to the right of code will be
    206   1.34  rillig 				 * placed this many indentation levels to the
    207   1.34  rillig 				 * left of code */
    208  1.148  rillig 	bool extra_expr_indent;	/* whether continuation lines from the
    209  1.124  rillig 				 * expression part of "if (e)", "while (e)",
    210  1.124  rillig 				 * "for (e; e; e)" should be indented an extra
    211  1.172  rillig 				 * tab stop so that they are not confused with
    212   1.34  rillig 				 * the code that follows */
    213  1.172  rillig 	bool else_if_in_same_line;
    214  1.148  rillig 	bool function_brace_split;	/* split function declaration and brace
    215  1.148  rillig 					 * onto separate lines */
    216  1.148  rillig 	bool format_col1_comments;	/* If comments which start in column 1
    217  1.148  rillig 					 * are to be reformatted (just like
    218  1.148  rillig 					 * comments that begin in later
    219  1.148  rillig 					 * columns) */
    220  1.148  rillig 	bool format_block_comments;	/* whether comments beginning with '/ *
    221  1.148  rillig 					 * \n' are to be reformatted */
    222  1.148  rillig 	bool indent_parameters;
    223  1.148  rillig 	int indent_size;	/* the size of one indentation level */
    224  1.148  rillig 	int block_comment_max_line_length;
    225  1.148  rillig 	int local_decl_indent;	/* like decl_indent but for locals */
    226  1.148  rillig 	bool lineup_to_parens_always;	/* whether to not(?) attempt to keep
    227   1.34  rillig 					 * lined-up code within the margin */
    228  1.148  rillig 	bool lineup_to_parens;	/* whether continued code within parens will be
    229  1.148  rillig 				 * lined up to the open paren */
    230  1.148  rillig 	bool proc_calls_space;	/* whether function calls look like: foo (bar)
    231   1.34  rillig 				 * rather than foo(bar) */
    232  1.172  rillig 	bool procnames_start_line;	/* whether the names of functions being
    233  1.172  rillig 					 * defined get placed in column 1 (i.e.
    234  1.172  rillig 					 * a newline is placed between the type
    235  1.172  rillig 					 * of the function and its name) */
    236  1.172  rillig 	bool space_after_cast;	/* "b = (int) a" vs. "b = (int)a" */
    237  1.148  rillig 	bool star_comment_cont;	/* whether comment continuation lines should
    238  1.140  rillig 				 * have stars at the beginning of each line */
    239  1.148  rillig 	bool swallow_optional_blanklines;
    240  1.148  rillig 	bool auto_typedefs;	/* whether to recognize identifiers ending in
    241   1.34  rillig 				 * "_t" like typedefs */
    242  1.148  rillig 	int tabsize;		/* the size of a tab */
    243  1.148  rillig 	int max_line_length;
    244  1.148  rillig 	bool use_tabs;		/* set true to use tabs for spacing, false uses
    245  1.148  rillig 				 * all spaces */
    246  1.173  rillig 	bool verbose;		/* print configuration to stderr */
    247   1.51  rillig } opt;
    248   1.31  rillig 
    249   1.34  rillig extern bool found_err;
    250   1.34  rillig extern bool had_eof;		/* whether input is exhausted */
    251   1.34  rillig extern int line_no;		/* the current line number. */
    252  1.144  rillig extern enum indent_enabled {
    253  1.148  rillig 	indent_on,
    254  1.148  rillig 	indent_off,
    255  1.148  rillig 	indent_last_off_line,
    256  1.144  rillig } indent_enabled;
    257   1.31  rillig 
    258   1.31  rillig #define	STACKSIZE 256
    259   1.31  rillig 
    260  1.110  rillig /* Properties of each level of parentheses or brackets. */
    261  1.110  rillig typedef struct paren_level_props {
    262  1.148  rillig 	int indent;		/* indentation of the operand/argument,
    263  1.110  rillig 				 * relative to the enclosing statement; if
    264  1.110  rillig 				 * negative, reflected at -1 */
    265  1.148  rillig 	enum paren_level_cast {
    266  1.148  rillig 		cast_unknown,
    267  1.148  rillig 		cast_maybe,
    268  1.148  rillig 		cast_no,
    269  1.148  rillig 	} cast;			/* whether the parentheses form a type cast */
    270  1.110  rillig } paren_level_props;
    271  1.110  rillig 
    272  1.177  rillig struct psym_stack {
    273  1.177  rillig 	int top;		/* pointer to top of stack */
    274  1.177  rillig 	parser_symbol sym[STACKSIZE];
    275  1.177  rillig 	int ind_level[STACKSIZE];
    276  1.177  rillig };
    277  1.177  rillig 
    278  1.129  rillig /*
    279  1.129  rillig  * The parser state determines the layout of the formatted text.
    280  1.129  rillig  *
    281  1.156  rillig  * At each '#if', the parser state is copied so that the corresponding '#else'
    282  1.156  rillig  * lines start in the same state.
    283  1.156  rillig  *
    284  1.129  rillig  * In a function body, the number of block braces determines the indentation
    285  1.129  rillig  * of statements and declarations.
    286  1.129  rillig  *
    287  1.129  rillig  * In a statement, the number of parentheses or brackets determines the
    288  1.129  rillig  * indentation of follow-up lines.
    289  1.129  rillig  *
    290  1.129  rillig  * In an expression, the token type determine whether to put spaces around.
    291  1.129  rillig  *
    292  1.129  rillig  * In a source file, the types of line determine the vertical spacing, such as
    293  1.129  rillig  * around preprocessing directives or function bodies, or above block
    294  1.129  rillig  * comments.
    295  1.129  rillig  */
    296   1.31  rillig extern struct parser_state {
    297  1.168  rillig 	lexer_symbol prev_lsym;	/* the previous token, but never comment,
    298  1.168  rillig 				 * newline or preprocessing line */
    299  1.136  rillig 
    300  1.148  rillig 	/* Token classification */
    301  1.136  rillig 
    302  1.148  rillig 	int quest_level;	/* when this is positive, we have seen a '?'
    303  1.136  rillig 				 * without the matching ':' in a '?:'
    304  1.136  rillig 				 * expression */
    305  1.151  rillig 	bool is_function_definition;	/* starts either at the 'name(' from a
    306  1.151  rillig 					 * function definition if it occurs at
    307  1.151  rillig 					 * the beginning of a line, or at the
    308  1.151  rillig 					 * first '*' from inside a declaration
    309  1.151  rillig 					 * when the line starts with words
    310  1.151  rillig 					 * followed by a '('; ends at the end
    311  1.151  rillig 					 * of that line */
    312  1.148  rillig 	bool block_init;	/* whether inside a block initialization */
    313  1.148  rillig 	int block_init_level;	/* the level of brace nesting in an
    314  1.136  rillig 				 * initialization */
    315  1.148  rillig 	bool init_or_struct;	/* whether there has been a type name and no
    316  1.136  rillig 				 * left parenthesis since the last semicolon.
    317  1.136  rillig 				 * When true, a '{' starts a structure
    318  1.136  rillig 				 * definition or an initialization list */
    319  1.148  rillig 	bool decl_on_line;	/* whether this line of code has part of a
    320  1.178  rillig 				 * declaration on it; used for indenting
    321  1.178  rillig 				 * comments */
    322  1.148  rillig 	bool in_stmt_or_decl;	/* whether in a statement or a struct
    323  1.136  rillig 				 * declaration or a plain declaration */
    324  1.178  rillig 	bool in_decl;		/* XXX: double-check the exact meaning */
    325  1.148  rillig 	bool in_func_def_params;
    326  1.174  rillig 	bool seen_case;		/* whether there was a 'case' or 'default', to
    327  1.174  rillig 				 * properly space the following ':' */
    328  1.148  rillig 	parser_symbol spaced_expr_psym;	/* the parser symbol to be shifted
    329  1.136  rillig 					 * after the parenthesized expression
    330  1.136  rillig 					 * from a 'for', 'if', 'switch' or
    331  1.136  rillig 					 * 'while'; or psym_0 */
    332  1.170  rillig 	parser_symbol lbrace_kind;	/* the kind of brace to be pushed to
    333  1.170  rillig 					 * the parser symbol stack next */
    334  1.136  rillig 
    335  1.148  rillig 	/* Indentation of statements and declarations */
    336  1.136  rillig 
    337  1.148  rillig 	int ind_level;		/* the indentation level for the line that is
    338  1.136  rillig 				 * currently prepared for output */
    339  1.148  rillig 	int ind_level_follow;	/* the level to which ind_level should be set
    340  1.136  rillig 				 * after the current line is printed */
    341  1.148  rillig 	bool in_stmt_cont;	/* whether the current line should have an
    342  1.137  rillig 				 * extra indentation level because we are in
    343  1.137  rillig 				 * the middle of a statement */
    344  1.148  rillig 	int decl_level;		/* current nesting level for a structure
    345  1.136  rillig 				 * declaration or an initializer */
    346  1.152  rillig 	int di_stack[20];	/* a stack of structure indentation levels */
    347  1.148  rillig 	bool decl_indent_done;	/* whether the indentation for a declaration
    348  1.136  rillig 				 * has been added to the code buffer. */
    349  1.148  rillig 	int decl_ind;		/* current indentation for declarations */
    350  1.148  rillig 	bool tabs_to_var;	/* true if using tabs to indent to var name */
    351  1.148  rillig 
    352  1.148  rillig 	enum {
    353  1.148  rillig 		eei_no,
    354  1.148  rillig 		eei_yes,
    355  1.148  rillig 		eei_last
    356  1.148  rillig 	} extra_expr_indent;
    357  1.148  rillig 
    358  1.177  rillig 	struct psym_stack psyms;
    359  1.136  rillig 
    360  1.148  rillig 	/* Spacing inside a statement or declaration */
    361  1.136  rillig 
    362  1.148  rillig 	bool next_unary;	/* whether the following operator should be
    363  1.124  rillig 				 * unary; is used in declarations for '*', as
    364  1.124  rillig 				 * well as in expressions */
    365  1.148  rillig 	bool want_blank;	/* whether the following token should be
    366   1.59  rillig 				 * prefixed by a blank. (Said prefixing is
    367   1.59  rillig 				 * ignored in some cases.) */
    368  1.148  rillig 	int line_start_nparen;	/* the number of parentheses or brackets that
    369  1.140  rillig 				 * were open at the beginning of the current
    370  1.140  rillig 				 * line; used to indent within statements,
    371  1.140  rillig 				 * initializers and declarations */
    372  1.148  rillig 	int nparen;		/* the number of parentheses or brackets that
    373  1.111  rillig 				 * are currently open; used to indent the
    374  1.111  rillig 				 * remaining lines of the statement,
    375  1.111  rillig 				 * initializer or declaration */
    376  1.175  rillig 	paren_level_props paren[20];
    377  1.158  rillig 	enum {
    378  1.174  rillig 		dp_start,	/* the beginning of a declaration */
    379  1.174  rillig 		dp_word,	/* seen a type name */
    380  1.174  rillig 		dp_word_asterisk,	/* seen a type name and some '*' */
    381  1.174  rillig 		dp_other,
    382  1.174  rillig 	} decl_ptr;		/* detects declarations like 'typename *x', to
    383  1.174  rillig 				 * prevent the '*' from being interpreted as a
    384  1.174  rillig 				 * binary operator */
    385  1.111  rillig 
    386  1.148  rillig 	/* Horizontal spacing for comments */
    387  1.136  rillig 
    388  1.148  rillig 	int comment_delta;	/* used to set up indentation for all lines of
    389   1.34  rillig 				 * a boxed comment after the first one */
    390  1.148  rillig 	int n_comment_delta;	/* remembers how many columns there were before
    391  1.148  rillig 				 * the start of a box comment so that
    392   1.31  rillig 				 * forthcoming lines of the comment are
    393   1.31  rillig 				 * indented properly */
    394  1.148  rillig 	int com_ind;		/* indentation of the current comment */
    395   1.59  rillig 
    396  1.148  rillig 	/* Vertical spacing */
    397   1.59  rillig 
    398  1.160  rillig 	bool break_after_comma;	/* whether to add a newline after the next
    399  1.160  rillig 				 * comma; used in declarations but not in
    400  1.160  rillig 				 * initializer lists */
    401  1.148  rillig 	bool force_nl;		/* whether the next token is forced to go to a
    402  1.141  rillig 				 * new line; used after 'if (expr)' and in
    403  1.140  rillig 				 * similar situations; tokens like '{' may
    404  1.140  rillig 				 * ignore this */
    405  1.128  rillig 
    406  1.148  rillig 	enum declaration {
    407  1.148  rillig 		decl_no,	/* no declaration anywhere nearby */
    408  1.148  rillig 		decl_begin,	/* collecting tokens of a declaration */
    409  1.148  rillig 		decl_end,	/* finished a declaration */
    410  1.148  rillig 	} declaration;
    411  1.148  rillig 	bool blank_line_after_decl;
    412  1.128  rillig 
    413  1.156  rillig 	/* Comments */
    414  1.156  rillig 
    415  1.156  rillig 	bool curr_col_1;	/* whether the current token started in column
    416  1.156  rillig 				 * 1 of the original input */
    417  1.156  rillig 	bool next_col_1;
    418  1.156  rillig } ps;
    419  1.156  rillig 
    420  1.156  rillig extern struct output_state {
    421  1.153  rillig 	enum line_kind {
    422  1.153  rillig 		lk_other,
    423  1.157  rillig 		lk_blank,
    424  1.153  rillig 		lk_if,		/* #if, #ifdef, #ifndef */
    425  1.153  rillig 		lk_endif,	/* #endif */
    426  1.157  rillig 		lk_stmt_head,	/* the ')' of an incomplete statement such as
    427  1.157  rillig 				 * 'if (expr)' or 'for (expr; expr; expr)' */
    428  1.154  rillig 		lk_func_end,	/* the last '}' of a function body */
    429  1.155  rillig 		lk_block_comment,
    430  1.164  rillig 		lk_case_or_default,
    431  1.157  rillig 	} line_kind;		/* kind of the line that is being prepared for
    432  1.157  rillig 				 * output; is reset to lk_other each time after
    433  1.157  rillig 				 * trying to send a line to the output, even if
    434  1.157  rillig 				 * that line was a suppressed blank line; used
    435  1.157  rillig 				 * for inserting or removing blank lines */
    436  1.157  rillig 	enum line_kind prev_line_kind;	/* the kind of line that was actually
    437  1.157  rillig 					 * sent to the output */
    438  1.153  rillig 
    439  1.156  rillig 	struct buffer indent_off_text;	/* text from between 'INDENT OFF' and
    440  1.156  rillig 					 * 'INDENT ON', both inclusive */
    441  1.156  rillig } out;
    442   1.31  rillig 
    443    1.3  rillig 
    444   1.59  rillig #define array_length(array) (sizeof(array) / sizeof((array)[0]))
    445    1.1   kamil 
    446    1.5  rillig #ifdef debug
    447  1.123  rillig void debug_printf(const char *, ...) __printflike(1, 2);
    448  1.123  rillig void debug_println(const char *, ...) __printflike(1, 2);
    449  1.159  rillig void debug_blank_line(void);
    450  1.133  rillig void debug_vis_range(const char *, const char *, size_t, const char *);
    451  1.159  rillig void debug_parser_state(void);
    452  1.123  rillig void debug_parse_stack(const char *);
    453  1.161  rillig void debug_print_buf(const char *, const struct buffer *);
    454  1.123  rillig void debug_buffers(void);
    455  1.123  rillig extern const char *const lsym_name[];
    456  1.123  rillig extern const char *const psym_name[];
    457  1.142  rillig extern const char *const paren_level_cast_name[];
    458  1.156  rillig extern const char *const line_kind_name[];
    459    1.9  rillig #else
    460  1.123  rillig #define debug_noop() do { } while (false)
    461  1.123  rillig #define	debug_printf(fmt, ...) debug_noop()
    462  1.123  rillig #define	debug_println(fmt, ...) debug_noop()
    463  1.159  rillig #define debug_blank_line() debug_noop()
    464  1.123  rillig #define	debug_vis_range(prefix, s, e, suffix) debug_noop()
    465  1.159  rillig #define	debug_parser_state() debug_noop()
    466  1.123  rillig #define	debug_parse_stack(situation) debug_noop()
    467  1.161  rillig #define debug_print_buf(name, buf) debug_noop()
    468  1.123  rillig #define	debug_buffers() debug_noop()
    469    1.5  rillig #endif
    470   1.67  rillig 
    471   1.74  rillig void register_typename(const char *);
    472   1.67  rillig int compute_code_indent(void);
    473   1.67  rillig int compute_label_indent(void);
    474  1.133  rillig int ind_add(int, const char *, size_t);
    475   1.94  rillig 
    476   1.79  rillig void inp_skip(void);
    477   1.79  rillig char inp_next(void);
    478   1.89  rillig 
    479   1.54  rillig lexer_symbol lexi(void);
    480  1.129  rillig void diag(int, const char *, ...) __printflike(2, 3);
    481  1.104  rillig void output_line(void);
    482   1.79  rillig void inp_read_line(void);
    483   1.49  rillig void parse(parser_symbol);
    484   1.34  rillig void process_comment(void);
    485   1.34  rillig void set_option(const char *, const char *);
    486  1.149  rillig void load_profile_files(const char *);
    487   1.34  rillig 
    488  1.135  rillig void *nonnull(void *);
    489   1.25  rillig 
    490  1.103  rillig void buf_add_char(struct buffer *, char);
    491  1.133  rillig void buf_add_chars(struct buffer *, const char *, size_t);
    492   1.29  rillig 
    493   1.29  rillig static inline bool
    494   1.98  rillig ch_isalnum(char ch)
    495   1.98  rillig {
    496  1.148  rillig 	return isalnum((unsigned char)ch) != 0;
    497   1.98  rillig }
    498   1.98  rillig 
    499   1.98  rillig static inline bool
    500   1.98  rillig ch_isalpha(char ch)
    501   1.98  rillig {
    502  1.148  rillig 	return isalpha((unsigned char)ch) != 0;
    503   1.98  rillig }
    504   1.98  rillig 
    505   1.98  rillig static inline bool
    506   1.62  rillig ch_isblank(char ch)
    507   1.29  rillig {
    508  1.148  rillig 	return ch == ' ' || ch == '\t';
    509   1.29  rillig }
    510   1.38  rillig 
    511   1.98  rillig static inline bool
    512   1.98  rillig ch_isdigit(char ch)
    513   1.98  rillig {
    514  1.148  rillig 	return '0' <= ch && ch <= '9';
    515   1.98  rillig }
    516   1.98  rillig 
    517   1.98  rillig static inline bool
    518   1.98  rillig ch_isspace(char ch)
    519   1.98  rillig {
    520  1.148  rillig 	return isspace((unsigned char)ch) != 0;
    521   1.98  rillig }
    522   1.98  rillig 
    523   1.38  rillig static inline int
    524   1.38  rillig next_tab(int ind)
    525   1.38  rillig {
    526  1.148  rillig 	return ind - ind % opt.tabsize + opt.tabsize;
    527   1.38  rillig }
    528