Skip to content

Commit

Permalink
Function pointers, Fix #26
Browse files Browse the repository at this point in the history
  • Loading branch information
jeff-hykin authored May 11, 2019
2 parents 2069636 + 1f37cdb commit cabf191
Show file tree
Hide file tree
Showing 8 changed files with 326 additions and 80 deletions.
4 changes: 2 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
todo.txt
.DS_Store
node_modules
.vscode/ipch
.vscode/settings.json
**/.vscode/ipch
**/.vscode/settings.json
node_modules
110 changes: 84 additions & 26 deletions cpp/generate.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,20 @@
match: /,/,
tag_as: "comma punctuation.separator.delimiter"
)
# TODO eventually move this outside of the # Utils section
ref_deref_definition_pattern = newPattern(
should_fully_match: [ '*', '&', '**', '&&', '*&', '*& ' ],
should_not_fully_match: [ '&*', '&&&' ],
match: newPattern(
match: zeroOrMoreOf(/\*/.maybe(@spaces)),
tag_as: "storage.modifier.pointer"
).then(
# TODO: make a readble regex way to do the {0,2} and replace this with it
match: /(?:\&\s*?){0,2}/,
tag_as: "storage.modifier.reference"
).maybe(@spaces)
)

def blockFinderFor( name:"", tag_as:"", start_pattern:nil, needs_semicolon: true, primary_includes: [], head_includes:[], body_includes: [ :$initial_context ], tail_includes: [ :$initial_context ], secondary_includes:[])
lookahead_endings = /[;>\[\]=]/
if needs_semicolon
Expand Down Expand Up @@ -89,7 +103,6 @@ def blockFinderFor( name:"", tag_as:"", start_pattern:nil, needs_semicolon: true
#
#
cpp_grammar[:$initial_context] = [
:parameter_struct, # TODO this is here because it needs to activate inside of function-pointer parameters. Once function-pointer syntax is implemented, remove it from here
:struct_declare,
:special_block_context,
:macro_argument,
Expand Down Expand Up @@ -548,6 +561,27 @@ def blockFinderFor( name:"", tag_as:"", start_pattern:nil, needs_semicolon: true
)
)
#
# Types
#
cpp_grammar[:qualified_type] = qualified_type = newPattern(
should_fully_match: ["A","A::B","A::B<C>::D<E>", "unsigned char","long long int", "unsigned short int","struct a"],
should_not_partial_match: ["return", "static const"],
match: maybe(@spaces).lookBehindToAvoid(/\w/).lookAheadFor(/\w/).lookAheadToAvoid(
@cpp_tokens.that(:isWord, not(:isType), not(:isTypeCreator)).lookAheadToAvoid(/[\w]/).maybe(@spaces)
).maybe(inline_attribute).maybe(@spaces)
.zeroOrMoreOf(newPattern(@cpp_tokens.that(:isTypeSpecifier).or(@cpp_tokens.that(:isTypeCreator))).then(@spaces))
.maybe(scope_resolution).maybe(@spaces).then(identifier).maybe(template_call.without_numbered_capture_groups).lookAheadToAvoid(/[\w<:.]/),
tag_as: "entity.name.type meta.qualified_type",
includes: [
newPattern(match: @cpp_tokens.that(:isTypeCreator), tag_as: "storage.type.$match"),
:function_type,
:storage_types,
:number_literal,
:string_context_c,
:comma,
],
)
#
# Functions
#
functionTemplate = ->(repository_name:nil, match_name: nil, tag_name_as: nil, tag_content_as: nil, tag_parenthese_as: nil) do
Expand Down Expand Up @@ -584,18 +618,8 @@ def blockFinderFor( name:"", tag_as:"", start_pattern:nil, needs_semicolon: true
).then(@spaces).then(
match: variable_name,
tag_as: "entity.name.type.struct",
).then(@spaces).zeroOrMoreOf(
match: /\*|&/.maybe(@spaces),
includes: [
newPattern(
match: /\*/,
tag_as: "keyword.operator.dereference"
),
newPattern(
match: /&/,
tag_as: "keyword.operator.reference"
),
]
).maybe(@spaces).then(
ref_deref_definition_pattern.or(@spaces)
).then(
match: variable_name,
tag_as: "variable.other.object.declare",
Expand All @@ -609,18 +633,8 @@ def blockFinderFor( name:"", tag_as:"", start_pattern:nil, needs_semicolon: true
).then(@spaces).then(
match: variable_name,
tag_as: "entity.name.type.struct.parameter",
).then(@spaces).zeroOrMoreOf(
match: /\*|&/.maybe(@spaces),
includes: [
newPattern(
match: /\*/,
tag_as: "keyword.operator.dereference"
),
newPattern(
match: /&/,
tag_as: "keyword.operator.reference"
),
]
).maybe(@spaces).then(
ref_deref_definition_pattern.or(@spaces)
# this is a maybe because its possible to have a type declare without an actual parameter
).maybe(
match: variable_name,
Expand Down Expand Up @@ -790,6 +804,50 @@ def blockFinderFor( name:"", tag_as:"", start_pattern:nil, needs_semicolon: true
}
]
#
# function pointer
#
array_brackets = newPattern(
match: /\[/,
tag_as: "punctuation.definition.begin.bracket.square"
).then(
match: /\w*/,
includes: [:evaluation_context]
).then(
match: /\]/,
tag_as: "punctuation.definition.end.bracket.square"
).maybe(@spaces)
after_declaration = maybe(@spaces).lookAheadFor(/[{=,);]|\n/).lookAheadToAvoid(/\(/)
cpp_grammar[:function_pointer] = PatternRange.new(
start_pattern: qualified_type.maybe(@spaces).then(ref_deref_definition_pattern).then(
match: /\(/,
tag_as: "punctuation.section.parens.begin.bracket.round.function.pointer"
).then(
match: /\*/,
tag_as: "punctuation.definition.function.pointer.dereference",
).maybe(@spaces).maybe(
match: identifier,
tag_as: "variable.other.definition.pointer.function"
).maybe(@spaces).zeroOrMoreOf(
# an array of function pointers ?
array_brackets
).then(
# closing ) for the variable name
match: /\)/,
tag_as: "punctuation.section.parens.end.bracket.round.function.pointer"
).maybe(@spaces).then(
# opening ( for the parameter types
match: /\(/,
tag_as: "punctuation.section.parameters.begin.bracket.round.function.pointer"
),
end_pattern: newPattern(
match: /\)/,
tag_as: "punctuation.section.parameters.end.bracket.round.function.pointer"
).then(after_declaration),
includes: [
:parameter_struct, :probably_a_parameter, :function_context_c,
]
)
#
# Probably a parameter
#
array_brackets = /\[\]/.maybe(@spaces)
Expand Down Expand Up @@ -1174,7 +1232,7 @@ def blockFinderFor( name:"", tag_as:"", start_pattern:nil, needs_semicolon: true
:template_call_range,
:comments_context,
],
body_includes: [ :constructor_context, :$initial_context ],
body_includes: [ :function_pointer, :constructor_context, :$initial_context ],
)
end
cpp_grammar[:class_block] = generateClassOrStructBlockFinder["class"]
Expand Down
12 changes: 10 additions & 2 deletions cpp/tags.txt
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ entity.name.function.preprocessor.cpp
entity.name.operator.overloadee.cpp
entity.name.tag.pragma-mark.cpp
entity.name.type.$3.cpp
entity.name.type.cpp
entity.name.type.enum.cpp
entity.name.type.inherited.cpp
entity.name.type.namespace.cpp
Expand Down Expand Up @@ -67,7 +68,6 @@ keyword.operator.decrement.cpp
keyword.operator.delete.array.bracket.cpp
keyword.operator.delete.array.cpp
keyword.operator.delete.cpp
keyword.operator.dereference.cpp
keyword.operator.functionlike.cpp
keyword.operator.increment.cpp
keyword.operator.logical.cpp
Expand All @@ -76,7 +76,6 @@ keyword.operator.minus.exponent.hexadecimal.cpp
keyword.operator.new.cpp
keyword.operator.plus.exponent.decimal.cpp
keyword.operator.plus.exponent.hexadecimal.cpp
keyword.operator.reference.cpp
keyword.operator.sizeof.cpp
keyword.operator.ternary.cpp
keyword.operator.typeid.cpp
Expand Down Expand Up @@ -143,6 +142,7 @@ meta.preprocessor.diagnostic.cpp
meta.preprocessor.include.cpp
meta.preprocessor.macro.cpp
meta.preprocessor.pragma.cpp
meta.qualified_type.cpp
meta.scope-resolution.cpp
meta.section.cpp
meta.static_assert.message.cpp
Expand All @@ -167,6 +167,7 @@ punctuation.definition.comment.cpp
punctuation.definition.comment.end.cpp
punctuation.definition.directive.cpp
punctuation.definition.end.bracket.square.cpp
punctuation.definition.function.pointer.dereference.cpp
punctuation.definition.initializer-list.parameters.cpp
punctuation.definition.lambda.return-type.cpp
punctuation.definition.parameters.begin.constructor.cpp
Expand Down Expand Up @@ -218,14 +219,18 @@ punctuation.section.block.end.bracket.curly.struct.cpp
punctuation.section.block.end.bracket.curly.switch.cpp
punctuation.section.block.end.bracket.curly.union.cpp
punctuation.section.parameters.begin.bracket.round.cpp
punctuation.section.parameters.begin.bracket.round.function.pointer.cpp
punctuation.section.parameters.begin.bracket.round.operator-overload.cpp
punctuation.section.parameters.end.bracket.round.cpp
punctuation.section.parameters.end.bracket.round.function.pointer.cpp
punctuation.section.parameters.end.bracket.round.operator-overload.cpp
punctuation.section.parens.begin.bracket.round.conditional.switch.cpp
punctuation.section.parens.begin.bracket.round.cpp
punctuation.section.parens.begin.bracket.round.function.pointer.cpp
punctuation.section.parens.begin.bracket.round.initialization.cpp
punctuation.section.parens.end.bracket.round.conditional.switch.cpp
punctuation.section.parens.end.bracket.round.cpp
punctuation.section.parens.end.bracket.round.function.pointer.cpp
punctuation.section.parens.end.bracket.round.initialization.cpp
punctuation.separator.attribute.cpp
punctuation.separator.case.cpp
Expand All @@ -248,6 +253,8 @@ punctuation.whitespace.comment.leading.cpp
storage.modifier.array.bracket.square.cpp
storage.modifier.cpp
storage.modifier.lambda.$0.cpp
storage.modifier.pointer.cpp
storage.modifier.reference.cpp
storage.modifier.specificer.functional.pre-parameters.$0.cpp
storage.modifier.specifier.$0.cpp
storage.modifier.specifier.functional.post-parameters.$0.cpp
Expand Down Expand Up @@ -283,6 +290,7 @@ support.type.posix-reserved.cpp
support.type.posix-reserved.pthread.cpp
variable.language.this.cpp
variable.other.cpp
variable.other.definition.pointer.function.cpp
variable.other.enummember.cpp
variable.other.macro.argument.cpp
variable.other.object.access.cpp
Expand Down
8 changes: 4 additions & 4 deletions cpp/tokens.rb
Original file line number Diff line number Diff line change
Expand Up @@ -106,11 +106,11 @@
{ representation: "auto" , name: "auto" , isPrimitive: true, isType: true},
{ representation: "void" , name: "void" , isPrimitive: true, isType: true},
{ representation: "char" , name: "char" , isPrimitive: true, isType: true},
{ representation: "short" , name: "short" , isPrimitive: true, isType: true},
{ representation: "short" , name: "short" , isPrimitive: true, isType: true, isTypeSpecifier: true},
{ representation: "int" , name: "int" , isPrimitive: true, isType: true},
{ representation: "signed" , name: "signed" , isPrimitive: true, isType: true},
{ representation: "unsigned" , name: "unsigned" , isPrimitive: true, isType: true},
{ representation: "long" , name: "long" , isPrimitive: true, isType: true},
{ representation: "signed" , name: "signed" , isPrimitive: true, isType: true, isTypeSpecifier: true},
{ representation: "unsigned" , name: "unsigned" , isPrimitive: true, isType: true, isTypeSpecifier: true},
{ representation: "long" , name: "long" , isPrimitive: true, isType: true, isTypeSpecifier: true},
{ representation: "float" , name: "float" , isPrimitive: true, isType: true},
{ representation: "double" , name: "double" , isPrimitive: true, isType: true},
{ representation: "bool" , name: "bool" , isPrimitive: true, isType: true},
Expand Down
2 changes: 1 addition & 1 deletion syntaxes/cpp.tmLanguage.json

Large diffs are not rendered by default.

Loading

0 comments on commit cabf191

Please sign in to comment.