const { commaSep, commaSep1 } = require('./common/common')
const base_types = require('./common/base_types')
const expr = require('./common/expr')
const literal = require('./common/literal')
const directive = require('./common/directive')
const interface_ = require('./common/interface')
const bitmask = require('./common/bitmask')
const annotation = require('./common/annotation')
const template = require('./common/template')
const value_type = require('./common/value_type')
const typedef_dcl = require('./common/typedef_dcl')
const union_dcl = require('./common/union_dcl')
const corba_interface = require('./common/corba_interface')
const corba_value = require('./common/corba_value')
const component_basic = require('./common/component_basic') const component_homes = require('./common/component_homes') const event_dcl = require('./common/event_dcl') const component_port = require('./common/component_port')
module.exports = grammar({
name: 'idl',
extras: $ => [/\s|\\\r?\n/, $.comment],
inline: $ => [],
precedences: $ => [
[$.annotation_appl, $.scoped_name],
[$.simple_type_spec, $.unary_expr],
[$.annotation_member_type, $.const_type],
[$.simple_type_spec, $.primary_expr],
[$.definition, $.type_dcl],
[$.value_inheritance_spec, $.value_supports],
[$.value_inheritance_spec, $.value_inheritance],
],
rules: {
specification: $ => repeat(choice($.preproc_call, $.definition)),
...base_types.rules,
...expr.rules,
...literal.rules,
...directive.rules,
...interface_.rules,
...bitmask.rules,
...annotation.rules,
...template.rules,
...value_type.rules,
...typedef_dcl.rules,
...union_dcl.rules,
...corba_interface.rules,
...corba_value.rules,
...component_basic.rules,
...component_homes.rules,
...event_dcl.rules,
...component_port.rules, definition: $ =>
choice(
$.predefine,
seq(
choice(
$.module_dcl,
$.type_dcl,
$.const_dcl,
$.typedef_dcl,
$.except_dcl,
$.interface_dcl,
$.annotation_dcl, $.template_module_dcl, $.template_module_inst, $.value_dcl, $.type_id_dcl, $.type_prefix_dcl, $.import_dcl, $.component_dcl, $.home_dcl, $.event_dcl, $.porttype_dcl, $.connector_dcl, ),
';',
),
),
type_dcl: $ => choice($.constr_type_dcl, $.native_dcl, $.typedef_dcl),
constr_type_dcl: $ =>
choice(
$.struct_dcl,
$.union_dcl,
$.enum_dcl,
$.bitset_dcl, $.bitmask_dcl, ),
native_dcl: $ => seq('native', $.simple_declarator),
module_dcl: $ =>
seq('module', $.identifier, '{', repeat($.definition), '}'),
struct_dcl: $ => choice($.struct_forward_dcl, $.struct_def),
struct_forward_dcl: $ => seq('struct', $.identifier),
struct_def: $ =>
seq(
repeat($.annotation_appl),
'struct',
$.identifier,
optional(seq(':', field('parent', $.scoped_name))),
'{',
repeat($.member),
'}',
),
member: $ =>
seq(
repeat($.annotation_appl),
field('type', $.type_spec),
field('identifier', $.declarators),
optional($.default),
';',
),
default: $ => seq('default', $.const_expr),
predefine: $ => seq('#define', /[^\n]*/),
const_dcl: $ => seq('const', $.const_type, $.identifier, '=', $.const_expr),
const_type: $ =>
choice(
$.integer_type,
$.floating_pt_type,
$.fixed_pt_const_type,
$.char_type,
$.boolean_type,
$.octet_type,
$.string_type,
$.scoped_name,
$.sequence_type,
),
identifier: _ => /\w[\w\d_]*/, comment: $ =>
choice(
seq('//', repeat1($.annotation_appl), /\r?\n/),
seq('//', /(\\+(.|\r?\n)|[^\\\n])*/),
seq('/*', /[^*]*\*+([^/*][^*]*\*+)*/, '/'),
),
},
})