Class: Textbringer::CMode

Inherits:
ProgrammingMode show all
Defined in:
lib/textbringer/modes/c_mode.rb

Constant Summary collapse

KEYWORDS =
%w(
  auto break case char const continue default double do
  else enum extern float for goto if inline int long
  register restrict return short signed sizeof static struct
  switch typedef union unsigned void volatile while _Bool
  _Complex _Imaginary
)
TOKEN_NAMES =
[
  :preprocessing_directive,
  :comment,
  :keyword,
  :identifier,
  :constant,
  :string_literal,
  :punctuator,
  :space,
  :partial_comment,
  :unknown
]
TOKEN_REGEXP =
/\G(?:
(?<preprocessing_directive>
  ^[ \t\f\v]*(?:\#|%:).*(?:\\\n.*)*[^\\]\n
) |
(?<comment>
  (?<multiline_comment> \/\* (?> (?:.|\n)*? \*\/ ) ) |
  (?<singleline_comment> \/\/ .*(?:\\\n.*)*(?<!\\)\n )
) |
(?<partial_comment>
  (?<partial_multiline_comment> \/\* (?:.|\n)* ) |
  (?<partial_singleline_comment> \/\/ .*? \\\n (?:.|\n)* )
) |
(?<keyword>
  (?: #{KEYWORDS.join("|")} ) \b
) |
(?<constant>
  (?<floating_constant>
(?<decimal_floating_constant>
  (?<fractional_constant>
    (?<digit_sequence> [0-9]+ )? \. \g<digit_sequence> |
    \g<digit_sequence> \. )
      (?<exponent_part> [eE] [+\-]? \g<digit_sequence> )?
      (?<floating_suffix> [flFL] )?
) |
(?<hexadecimal_floating_constant>
  (?<hexadecimal_prefix> 0x | 0X )
      (?<hexadecimal_fractional_constant>
        (?<hexadecimal_digit_sequence> [0-9a-fA-F]+ )? \.
            \g<hexadecimal_digit_sequence> |
        \g<hexadecimal_digit_sequence> \. )
      (?<binary_exponent_part> [pP] [+\-]? \g<digit_sequence> )
      \g<floating_suffix>? |
  \g<hexadecimal_prefix> \g<hexadecimal_digit_sequence>
      \g<binary_exponent_part> \g<floating_suffix>?
)
  ) |
  (?<integer_constant>
(?<decimal_constant> [1-9][0-9]* )
(?<integer_suffix>
  (?<unsigned_suffix> [uU] ) (?<long_suffix> [lL] )?
  \g<unsigned_suffix> (?<long_long_suffix> ll | LL )?
  \g<long_suffix> \g<unsigned_suffix>?
  \g<long_long_suffix> \g<unsigned_suffix>?
)? |
(?<hexadecimal_constant>
  \g<hexadecimal_prefix> \g<hexadecimal_digit_sequence> )
      \g<integer_suffix>? |
(?<octal_constant> 0 (?<octal_digit> [0-7] )* )
    \g<integer_suffix>?
  ) |
  (?<character_constant>
' (?<c_char_sequence>
    (?<c_char>
      [^'\\\r\n] |
      (?<escape_sequence>
        (?<simple_escape_sequence> \\ ['"?\\abfnrtv] ) |
        (?<octal_escape_sequence> \\ \g<octal_digit>{1,3} ) |
        (?<hexadecimal_escape_sequence>
          \\x \g<hexadecimal_digit_sequence> ) |
        (?<universal_character_name>
          \\u[0-9a-fA-F]{4} |
          \\U[0-9a-fA-F]{8} )
      )
    )+
  ) ' |
L' \g<c_char_sequence> '
  )
) |
(?<string_literal>
  " (?<s_char_sequence>
  (?<s_char> [^"\\\r\n] | \g<escape_sequence> )+ ) " |
  L" \g<s_char_sequence>? "
) |
(?<identifier>
  (?<identifier_nondigit>
[_a-zA-Z] |
\g<universal_character_name> )
    (?: \g<identifier_nondigit> | [0-9] )*
) |
(?<punctuator>
  \[   |   \]   |   \(   |   \)   |   \{   |   \}   |
  \.\.\.   |   \.   |
  \+\+   |   \+=   |   \+   |
  ->   |   --   |   -=   |   -   |
  \*=   |   \*   |
  \/=   |   \/   |
  &&   |   &=   |   &   |
  \|\|   |   \|=   |   \|   |
  !=   |   !   |
  ~   |
  ==   |   =   |
  \^=   |   \^   |
  <:   |   <%   |   <<=   |   <<   |   <=   |   <   |
  >>=   |   >>   |   >=   |   >   |
  \?   |   ;   |
  :>   |   :   |
  ,   |
  \#\#   |   \#   |
  %>   |   %:%:   |   %:   |   %=   |   %
) |
(?<space>
  \s+
) |
(?<unknown>.)
)/x

Constants inherited from Mode

Mode::DEFAULT_SYNTAX_TABLE

Constants included from Commands

Textbringer::Commands::CLIPBOARD_AVAILABLE, Textbringer::Commands::CTAGS, Textbringer::Commands::HELP_RING, Textbringer::Commands::ISEARCH_STATUS, Textbringer::Commands::KEYBOARD_MACROS, Textbringer::Commands::REGISTERS, Textbringer::Commands::RE_SEARCH_STATUS

Constants included from Utils

Utils::COMPLETION, Utils::HOOKS

Instance Attribute Summary

Attributes inherited from Mode

#buffer

Instance Method Summary collapse

Methods inherited from ProgrammingMode

#indent_line, #indent_new_comment_line, #indent_region, #reindent_then_newline_and_indent

Methods inherited from Mode

define_generic_command, define_local_command, define_syntax, inherited, list, #name, #syntax_table

Methods included from Commands

[], #command_help, command_table, #current_prefix_arg, define_command, #execute_keyboard_macro, #get_tags, #isearch_done, #isearch_mode, #isearch_mode?, #isearch_pre_command_hook, #isearch_prompt, #isearch_repeat, #isearch_repeat_backward, #isearch_repeat_forward, #isearch_search, #keymap_bindings, list, #match_beginning, #match_end, #match_string, #number_prefix_arg, #prefix_numeric_value, #read_input_method_name, #read_keyboard_macro, #read_register, #replace_match, undefine_command, #universal_argument_mode

Methods included from Utils

add_hook, background, complete_for_minibuffer, foreground, foreground!, message, read_buffer, read_char, read_command_name, read_encoding, read_event, read_expression, read_file_name, read_from_minibuffer, read_key_sequence, read_object, read_single_char, received_keyboard_quit?, remove_hook, ruby_install_name, run_hooks, self_insert_and_exit_minibuffer, set_transient_map, show_exception, sit_for, sleep_for, y_or_n?, yes_or_no?

Constructor Details

#initialize(buffer) ⇒ CMode

Returns a new instance of CMode.



41
42
43
44
45
# File 'lib/textbringer/modes/c_mode.rb', line 41

def initialize(buffer)
  super(buffer)
  @buffer[:indent_level] = CONFIG[:c_indent_level]
  @buffer[:indent_tabs_mode] = CONFIG[:c_indent_tabs_mode]
end

Instance Method Details

#comment_startObject



37
38
39
# File 'lib/textbringer/modes/c_mode.rb', line 37

def comment_start
  "//"
end

#compile(cmd = read_from_minibuffer("Compile: ", default: default_compile_command)) ⇒ Object



47
48
49
50
51
# File 'lib/textbringer/modes/c_mode.rb', line 47

def compile(cmd = read_from_minibuffer("Compile: ",
                                       default: default_compile_command))
  shell_execute(cmd, buffer_name: "*Compile result*",
                mode: BacktraceMode)
end

#default_compile_commandObject



57
58
59
# File 'lib/textbringer/modes/c_mode.rb', line 57

def default_compile_command
  "make"
end

#lex(s) ⇒ Object



180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
# File 'lib/textbringer/modes/c_mode.rb', line 180

def lex(s)
  tokens = []
  line = 1
  column = 0
  s.scan(TOKEN_REGEXP) do
    text = $&
    token_name = TOKEN_NAMES.find { |name| $~[name] }
    if text.empty?
      raise EditorError, "Empty token: (#{line},#{column}) #{$~.inspect}"
    end
    tokens.push([[line, column], token_name, CANONICAL_PUNCTUATORS[text]])
    lf_count = text.count("\n")
    if lf_count > 0
      line += lf_count
      column = text.slice(/[^\n]*\z/).size
    else
      column += text.size
    end
  end
  tokens
end

#symbol_patternObject



53
54
55
# File 'lib/textbringer/modes/c_mode.rb', line 53

def symbol_pattern
  /[A-Za-z0-9\_\\]/
end