Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7421387
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T08:23:58+00:00 2026-05-29T08:23:58+00:00

I am working on splitting a source file into tokens, particularly scanning for identifiers.

  • 0

I am working on splitting a source file into tokens, particularly scanning for identifiers. However, there is a requirement that identifiers be AT MOST 30 characters long. When an identifier reaches this length, I raise an exception with the message: 'Identifiers can only be 30 characters long, truncating..'.

This is how it should be, but when I raise this exception I jump out my method that scans for identifiers before I am able to store it. I need to somehow raise the exception AND keep the identifier that I have collected so far. Any ideas as to how this could be done?

# classify each character, and call approriate scan methods
def tokenize()
  @infile.each_char do |c|
    begin
      case c
      when /[a-zA-Z\$]/
        scan_identifier(c)
      when /\s/ 
        #ignore spaces
      else
        #do nothing
      end
    rescue TokenizerError => te
      puts "#{te.class}: #{te.message}"
    end
  end
end

# Reads an identifier from the source program
def scan_identifier(id)
  this_id = id #initialize this identifier with the character read above

  @infile.each_char do |c|
    if c =~ /[a-zA-Z0-9_]/
      this_id += c 
      # raising this exception leaves this function before collecting the 
      # truncated identifier
      raise TokenizerError, 'Identifiers can only be 30 characters long, truncating..' if this_id.length == 30
    else 
      puts "#{this_id}"
      break # not part of the identifier, or an error
    end
  end
end
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-29T08:23:59+00:00Added an answer on May 29, 2026 at 8:23 am

    This is an abuse of exceptions, IMO, because this is not an exceptional case. Instead, consider simply logging something:

        if c =~ /[a-zA-Z0-9_]/
          warn "Identifer was too long and was truncated"
          this_id += c 
    

    If you must use the exception for some reason, then the most straightforward way is just to put this_id in an instance variable instead:

    @this_identifier = id
    # ...
    

    Then, when you break in the rescue, just have the last expression be @this_identifier to return that value (yuck).


    Bonus comment: this is a truly wretched way to parse source files. You should be using something like RubyParser if you’re parsing Ruby, or Treetop if you’re parsing something else.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am working on splitting out an existing, working application that I currently have
I am working on splitting an MVC3 Application into two Areas. The existing application
I am currently working on a project that involves splitting a url. I have
I'm re-working an ant build file that we've been using on our project for
I am working on splitting code into smaller files and refactoring it a bit.
I'm working on an application controller for a program that is spitting text directly
Working with a SqlCommand in C# I've created a query that contains a IN
Working on a project that parses a log of events, and then updates a
I'm working on a software that does extensive queries to a database which is
Can someone please help me out? My code for splitting the strings is working

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.