Skip to content

Commit

Permalink
Add alternate root for grammar
Browse files Browse the repository at this point in the history
  • Loading branch information
XVilka committed May 14, 2021
1 parent f05e279 commit fed7bd0
Show file tree
Hide file tree
Showing 2 changed files with 182 additions and 1 deletion.
16 changes: 15 additions & 1 deletion grammar.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,13 +45,27 @@ module.exports = grammar({
[$._type_specifier, $._expression],
[$._type_specifier, $._expression, $.macro_type_specifier],
[$._type_specifier, $.macro_type_specifier],
[$.type_expression, $._abstract_declarator],
[$.type_expression],
[$.sized_type_specifier],
],

word: $ => $.identifier,

rules: {
translation_unit: $ => repeat($._top_level_item),
translation_unit: $ => choice(
repeat1($.type_expression),
repeat1($._top_level_item)
),

type_expression: $ => seq(
'__TYPE_EXPRESSION',
repeat($.type_qualifier),
field('type', $._type_specifier),
repeat($.abstract_pointer_declarator),
repeat($.abstract_array_declarator),
repeat($.abstract_pointer_declarator),
),

_top_level_item: $ => choice(
$.function_definition,
Expand Down
167 changes: 167 additions & 0 deletions test/corpus/baretypes.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,167 @@
========================================
Primitive types
========================================

__TYPE_EXPRESSION int
__TYPE_EXPRESSION uint8_t
__TYPE_EXPRESSION uint16_t
__TYPE_EXPRESSION uint32_t
__TYPE_EXPRESSION uint64_t
__TYPE_EXPRESSION uintptr_t

__TYPE_EXPRESSION int8_t
__TYPE_EXPRESSION int16_t
__TYPE_EXPRESSION int32_t
__TYPE_EXPRESSION int64_t
__TYPE_EXPRESSION intptr_t

__TYPE_EXPRESSION char16_t
__TYPE_EXPRESSION char32_t

__TYPE_EXPRESSION size_t
__TYPE_EXPRESSION ssize_t

---

(translation_unit
(type_expression (primitive_type))
(type_expression (primitive_type))
(type_expression (primitive_type))
(type_expression (primitive_type))
(type_expression (primitive_type))
(type_expression (primitive_type))
(type_expression (primitive_type))
(type_expression (primitive_type))
(type_expression (primitive_type))
(type_expression (primitive_type))
(type_expression (primitive_type))
(type_expression (primitive_type))
(type_expression (primitive_type))
(type_expression (primitive_type))
(type_expression (primitive_type)))

========================================
Pointer types
========================================

__TYPE_EXPRESSION int *
__TYPE_EXPRESSION uint8_t *
__TYPE_EXPRESSION uint16_t *
__TYPE_EXPRESSION uint32_t *
__TYPE_EXPRESSION uint64_t *
__TYPE_EXPRESSION uintptr_t *

__TYPE_EXPRESSION int8_t *
__TYPE_EXPRESSION int16_t *
__TYPE_EXPRESSION int32_t *
__TYPE_EXPRESSION int64_t *
__TYPE_EXPRESSION intptr_t *

__TYPE_EXPRESSION char16_t *
__TYPE_EXPRESSION char32_t *

__TYPE_EXPRESSION size_t *
__TYPE_EXPRESSION ssize_t *

__TYPE_EXPRESSION void *

---

(translation_unit
(type_expression (primitive_type) (abstract_pointer_declarator))
(type_expression (primitive_type) (abstract_pointer_declarator))
(type_expression (primitive_type) (abstract_pointer_declarator))
(type_expression (primitive_type) (abstract_pointer_declarator))
(type_expression (primitive_type) (abstract_pointer_declarator))
(type_expression (primitive_type) (abstract_pointer_declarator))
(type_expression (primitive_type) (abstract_pointer_declarator))
(type_expression (primitive_type) (abstract_pointer_declarator))
(type_expression (primitive_type) (abstract_pointer_declarator))
(type_expression (primitive_type) (abstract_pointer_declarator))
(type_expression (primitive_type) (abstract_pointer_declarator))
(type_expression (primitive_type) (abstract_pointer_declarator))
(type_expression (primitive_type) (abstract_pointer_declarator))
(type_expression (primitive_type) (abstract_pointer_declarator))
(type_expression (primitive_type) (abstract_pointer_declarator))
(type_expression (primitive_type) (abstract_pointer_declarator)))

========================================
Array types
========================================

__TYPE_EXPRESSION int [5]
__TYPE_EXPRESSION uint8_t [2]
__TYPE_EXPRESSION uint16_t [5]
__TYPE_EXPRESSION uint32_t [20]
__TYPE_EXPRESSION uint64_t [0]
__TYPE_EXPRESSION uintptr_t []

---

(translation_unit
(type_expression (primitive_type) (abstract_array_declarator (number_literal)))
(type_expression (primitive_type) (abstract_array_declarator (number_literal)))
(type_expression (primitive_type) (abstract_array_declarator (number_literal)))
(type_expression (primitive_type) (abstract_array_declarator (number_literal)))
(type_expression (primitive_type) (abstract_array_declarator (number_literal)))
(type_expression (primitive_type) (abstract_array_declarator)))

========================================
Array pointer types
========================================

__TYPE_EXPRESSION int* [5]
__TYPE_EXPRESSION uint8_t* [2]
__TYPE_EXPRESSION uint16_t* [5]
__TYPE_EXPRESSION uint32_t* [20]
__TYPE_EXPRESSION uint64_t* [0]
__TYPE_EXPRESSION uintptr_t* []

---

(translation_unit
(type_expression (primitive_type) (abstract_pointer_declarator (abstract_array_declarator (number_literal))))
(type_expression (primitive_type) (abstract_pointer_declarator (abstract_array_declarator (number_literal))))
(type_expression (primitive_type) (abstract_pointer_declarator (abstract_array_declarator (number_literal))))
(type_expression (primitive_type) (abstract_pointer_declarator (abstract_array_declarator (number_literal))))
(type_expression (primitive_type) (abstract_pointer_declarator (abstract_array_declarator (number_literal))))
(type_expression (primitive_type) (abstract_pointer_declarator (abstract_array_declarator))))

========================================
Array pointer types with qualifiers
========================================

__TYPE_EXPRESSION const int* [5]
__TYPE_EXPRESSION volatile uint8_t* [2]
__TYPE_EXPRESSION const uint16_t* [5]
__TYPE_EXPRESSION const uintptr_t* []

---

(translation_unit
(type_expression (type_qualifier) (primitive_type) (abstract_pointer_declarator (abstract_array_declarator (number_literal))))
(type_expression (type_qualifier) (primitive_type) (abstract_pointer_declarator (abstract_array_declarator (number_literal))))
(type_expression (type_qualifier) (primitive_type) (abstract_pointer_declarator (abstract_array_declarator (number_literal))))
(type_expression (type_qualifier) (primitive_type) (abstract_pointer_declarator (abstract_array_declarator))))

========================================
Complex types
========================================

__TYPE_EXPRESSION struct s1 *

__TYPE_EXPRESSION struct s2 {
int x;
float y : 5;
} [5]

---

(translation_unit
(type_expression (struct_specifier (type_identifier)) (abstract_pointer_declarator))
(type_expression (struct_specifier (type_identifier) (field_declaration_list
(field_declaration (primitive_type) (field_identifier)) (field_declaration (primitive_type)
(field_identifier) (bitfield_clause (number_literal))))) (abstract_array_declarator
(number_literal))))


0 comments on commit fed7bd0

Please sign in to comment.