Skip to content

Commit f71a7fd

Browse files
committed
Add type_reference and module_reference parsers
Rename title_case_identifier to type_reference. Add module_reference.
1 parent b0cecea commit f71a7fd

File tree

5 files changed

+55
-28
lines changed

5 files changed

+55
-28
lines changed

rasn-compiler/src/lexer/choice.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ pub fn selection_type_choice(input: Input<'_>) -> ParserResult<'_, ASN1Type> {
5151
into(separated_pair(
5252
skip_ws_and_comments(value_reference),
5353
skip_ws_and_comments(char(LEFT_CHEVRON)),
54-
skip_ws_and_comments(title_case_identifier),
54+
skip_ws_and_comments(type_reference),
5555
)),
5656
ASN1Type::ChoiceSelectionType,
5757
)

rasn-compiler/src/lexer/common.rs

Lines changed: 38 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,34 @@ pub fn block_comment(input: Input<'_>) -> ParserResult<'_, &str> {
5858
.parse(input)
5959
}
6060

61+
/// Parses a typereference lexical item.
62+
///
63+
/// #### X.680 12.2 Type references
64+
/// _A "typereference" shall consist of an arbitrary number (one or more) of letters, digits, and
65+
/// hyphens. The initial character shall be an upper-case letter. A hyphen shall not be the last
66+
/// character. A hyphen shall not be immediately followed by another hyphen. NOTE – The rules
67+
/// concerning hyphen are designed to avoid ambiguity with (possibly following) comment. 12.2.2A
68+
/// "typereference" shall not be one of the reserved character sequences listed in 12.38._
69+
pub fn type_reference(input: Input<'_>) -> ParserResult<'_, &'_ str> {
70+
map_res(
71+
recognize(pair(
72+
one_of("ABCDEFGHIJKLMNOPQRSTUVWXYZ"),
73+
many0(alt((
74+
preceded(char('-'), into_inner(alphanumeric1)),
75+
into_inner(alphanumeric1),
76+
))),
77+
)),
78+
|identifier| {
79+
if ASN1_KEYWORDS.contains(&identifier.inner()) {
80+
Err(MiscError("Identifier is ASN.1 keyword."))
81+
} else {
82+
Ok(identifier.into_inner())
83+
}
84+
},
85+
)
86+
.parse(input.clone())
87+
}
88+
6189
/// Parses an ASN1 identifier.
6290
///
6391
/// * `input` [Input]-wrapped string slice reference used as an input for the lexer
@@ -97,33 +125,23 @@ pub fn value_reference(input: Input<'_>) -> ParserResult<'_, &'_ str> {
97125
.parse(input)
98126
}
99127

128+
/// Parses a modulereference lexical item.
129+
///
130+
/// #### X.680 12.5 Module references
131+
/// _A "modulereference" shall consist of the sequence of characters specified for a
132+
/// "typereference" in 12.2. In analyzing an instance of use of this notation, a "modulereference"
133+
/// is distinguished from a "typereference" by the context in which it appears._
134+
pub fn module_reference(input: Input<'_>) -> ParserResult<'_, &'_ str> {
135+
type_reference(input)
136+
}
137+
100138
pub fn into_inner<'a, F>(parser: F) -> impl Parser<Input<'a>, Output = &'a str, Error = F::Error>
101139
where
102140
F: Parser<Input<'a>, Output = Input<'a>>,
103141
{
104142
map(parser, Input::into_inner)
105143
}
106144

107-
pub fn title_case_identifier(input: Input<'_>) -> ParserResult<'_, &str> {
108-
map_res(
109-
recognize(pair(
110-
one_of("ABCDEFGHIJKLMNOPQRSTUVWXYZ"),
111-
many0(alt((
112-
preceded(char('-'), into_inner(alphanumeric1)),
113-
into_inner(alphanumeric1),
114-
))),
115-
)),
116-
|identifier| {
117-
if ASN1_KEYWORDS.contains(&identifier.inner()) {
118-
Err(MiscError("Identifier is ASN.1 keyword."))
119-
} else {
120-
Ok(identifier.into_inner())
121-
}
122-
},
123-
)
124-
.parse(input.clone())
125-
}
126-
127145
pub fn skip_ws<'a, F>(inner: F) -> impl Parser<Input<'a>, Output = F::Output, Error = F::Error>
128146
where
129147
F: Parser<Input<'a>>,

rasn-compiler/src/lexer/macros.rs

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ use crate::intermediate::{
1212
GREATER_THAN, LESS_THAN, MACRO, PIPE,
1313
};
1414
use crate::lexer::common::{
15-
in_parentheses, skip_ws_and_comments, title_case_identifier, uppercase_identifier,
15+
in_parentheses, module_reference, skip_ws_and_comments, type_reference, uppercase_identifier,
1616
value_reference,
1717
};
1818
use crate::lexer::error::{MiscError, ParserResult};
@@ -188,7 +188,7 @@ fn supporting_productions(input: Input<'_>) -> ParserResult<'_, Vec<Production>>
188188
/// ```
189189
fn external_macro_reference(input: Input<'_>) -> ParserResult<'_, (&'_ str, &'_ str)> {
190190
separated_pair(
191-
title_case_identifier,
191+
module_reference,
192192
skip_ws_and_comments(char(DOT)),
193193
skip_ws_and_comments(macro_reference),
194194
)
@@ -502,8 +502,12 @@ fn macro_reference(input: Input<'_>) -> ParserResult<'_, &'_ str> {
502502
}
503503

504504
/// Parse a production reference.
505+
///
506+
/// #### X.680 1994 J.2.2 Productionreference
507+
/// _A "productionreference" shall consist of the sequence of characters specified for a
508+
/// "typereference" in 9.2._
505509
fn production_reference(input: Input<'_>) -> ParserResult<'_, &'_ str> {
506-
map_res(title_case_identifier, |v| {
510+
map_res(type_reference, |v| {
507511
if ADDITIONAL_KEYWORDS.contains(&v) {
508512
Err(MiscError(
509513
"Production reference can not be a keyword when used in ASN.1 MACRO.",
@@ -516,8 +520,13 @@ fn production_reference(input: Input<'_>) -> ParserResult<'_, &'_ str> {
516520
}
517521

518522
/// Parse a local type reference.
523+
///
524+
/// #### X.680 1994 J.2.3 Localtypereference
525+
/// _A "localtypereference" shall consist of the sequence of characters specified for a
526+
/// "typereference" in 9.2. A "localtypereference" is used as an identifier for types which are
527+
/// recognized during syntax analysis of an instance of the new type or value notation._
519528
fn local_type_reference(input: Input<'_>) -> ParserResult<'_, &'_ str> {
520-
map_res(title_case_identifier, |v| {
529+
map_res(type_reference, |v| {
521530
if ADDITIONAL_KEYWORDS.contains(&v) {
522531
Err(MiscError(
523532
"Type reference can not be a keyword when used in ASN.1 MACRO.",

rasn-compiler/src/lexer/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ fn end(input: Input<'_>) -> ParserResult<'_, &str> {
127127
pub fn top_level_type_declaration(input: Input<'_>) -> ParserResult<'_, ToplevelTypeDefinition> {
128128
into((
129129
skip_ws(many0(comment)),
130-
skip_ws(title_case_identifier),
130+
skip_ws(type_reference),
131131
opt(parameterization),
132132
preceded(assignment, pair(opt(asn_tag), asn1_type)),
133133
))
@@ -220,7 +220,7 @@ pub fn elsewhere_declared_type(input: Input<'_>) -> ParserResult<'_, ASN1Type> {
220220
identifier,
221221
tag(".&"),
222222
))))),
223-
skip_ws_and_comments(title_case_identifier),
223+
skip_ws_and_comments(type_reference),
224224
opt(skip_ws_and_comments(constraint)),
225225
),
226226
|(parent, id, constraints)| {

rasn-compiler/src/lexer/sequence.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ pub fn sequence_component(input: Input<'_>) -> ParserResult<'_, SequenceComponen
116116
tag(COMPONENTS_OF),
117117
skip_ws_and_comments(alt((
118118
into_inner(recognize(separated_list1(tag(".&"), identifier))),
119-
title_case_identifier,
119+
type_reference,
120120
))),
121121
),
122122
|id| SequenceComponent::ComponentsOf(id.into()),

0 commit comments

Comments
 (0)