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 ProgrammingMode

ProgrammingMode::PROGRAMMING_MODE_MAP

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_MODE_MAP, Textbringer::Commands::ISEARCH_STATUS, Textbringer::Commands::KEYBOARD_MACROS, Textbringer::Commands::REGISTERS, Textbringer::Commands::RE_SEARCH_STATUS, Textbringer::Commands::UNIVERSAL_ARGUMENT_MAP

Constants included from Utils

Utils::COMPLETION, Utils::HOOKS, Utils::Y_OR_N_MAP

Instance Attribute Summary

Attributes inherited from Mode

#buffer

Instance Method Summary collapse

Methods inherited from ProgrammingMode

#indent_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_pre_command_hook, #isearch_prompt, #isearch_repeat, #isearch_repeat_backward, #isearch_repeat_forward, #isearch_search, list, #match_beginning, #match_end, #match_string, #number_prefix_arg, #prefix_numeric_value, #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_file_name, read_from_minibuffer, read_key_sequence, 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.



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

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

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



43
44
45
46
47
# File 'lib/textbringer/modes/c_mode.rb', line 43

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

#default_compile_commandObject



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

def default_compile_command
  "make"
end

#lex(s) ⇒ Object



176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
# File 'lib/textbringer/modes/c_mode.rb', line 176

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, 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



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

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