-
Notifications
You must be signed in to change notification settings - Fork 139
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Introduce StringQuery to provide methods to access some metadata about the Ruby lexer.
- Loading branch information
Showing
8 changed files
with
409 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
# frozen_string_literal: true | ||
|
||
module Prism | ||
# Query methods that allow categorizing strings based on their context for | ||
# where they could be valid in a Ruby syntax tree. | ||
class StringQuery | ||
# The string that this query is wrapping. | ||
attr_reader :string | ||
|
||
# Initialize a new query with the given string. | ||
def initialize(string) | ||
@string = string | ||
end | ||
|
||
# Whether or not this string is a valid local variable name. | ||
def local? | ||
StringQuery.local?(string) | ||
end | ||
|
||
# Whether or not this string is a valid constant name. | ||
def constant? | ||
StringQuery.constant?(string) | ||
end | ||
|
||
# Whether or not this string is a valid method name. | ||
def method_name? | ||
StringQuery.method_name?(string) | ||
end | ||
|
||
# When using the FFI backend, we need to define the class-level methods | ||
# using the FFI library. | ||
if BACKEND == :FFI | ||
class << self | ||
# Mirrors the C extension's StringQuery::local? method. | ||
def local?(string) | ||
query(LibRubyParser.pm_string_query_local(string, string.bytesize, string.encoding.name)) | ||
end | ||
|
||
# Mirrors the C extension's StringQuery::constant? method. | ||
def constant?(string) | ||
query(LibRubyParser.pm_string_query_constant(string, string.bytesize, string.encoding.name)) | ||
end | ||
|
||
# Mirrors the C extension's StringQuery::method_name? method. | ||
def method_name?(string) | ||
query(LibRubyParser.pm_string_query_method_name(string, string.bytesize, string.encoding.name)) | ||
end | ||
|
||
private | ||
|
||
# Parse the enum result and return an appropriate boolean. | ||
def query(result) | ||
case result | ||
when :PM_STRING_QUERY_ERROR then raise ArgumentError, "Invalid or non ascii-compatible encoding" | ||
when :PM_STRING_QUERY_FALSE then false | ||
when :PM_STRING_QUERY_TRUE then true | ||
end | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.